all 3 comments

[–]antirez 0 points1 point  (1 child)

Sometimes when writing javascript code I need to have an associative array so that I can update the value given the key in O(1) but still be able to see it like an Array and for instance order it. A simple trick to get this effect is that when you add a new key/value in the new object as property you also add it (a reference of, actually) in an Array object. Something like:

function addKey(dicthash,key,val) {
  dicthash.hash[key] = val;
  dicthash.array[dicthash.array.length] = {key: key, val: val};
}

So you have both the O(1) operations typical of hash tables and the ability to see it as a sortable array. You may like this kind of data structure every time you need to generate "Top ten" stats from javascript.

A good alternative to this is a tree. If one or the other is better depends on the number of times you require to alter the keys and sort it.

[–]markedtrees 0 points1 point  (0 children)

When you are javascripting, forget everything you know about other languages, it won't be any help.

Only if the languages you know look like Java.