4

For example I already have an object filled with ordered key-values, I can iterate its keys forwards with for in statement:

var keyvalues = {"key1":"value1","key2":"value2","key3":"value3"};
for(var key in keyvalues)
{
   var value = keyvalues[key];
   .............
}

Now I want to iterate the keys backwards like 'key3', 'key2', 'key1', is there a magic way to do this?

diwatu
  • 5,641
  • 5
  • 38
  • 61
  • Enumerating objects doesn't guarantee a specific *order*... You can use an array if you need to keep them in a specific order. – brbcoding Jul 11 '13 at 18:08
  • [There is also one discussion / post.](http://stackoverflow.com/questions/3586304/how-to-reverse-the-order-in-a-for-loop) – Niklas Jul 11 '13 at 18:09
  • Also something to look at: http://stackoverflow.com/questions/1159277/array-flip-in-javascript – DevlshOne Jul 11 '13 at 18:09

2 Answers2

9

There is no guarantee of order in a for... in loop.

Wait, what?

There is no logic in 'order' of object keys. If you'd like, you can grab them and then use .sort on them. The language specification tells us this explicitly in the for...in iteration algorithm:

The mechanics and order of enumerating the properties (step 6.a in the first algorithm, step 7.a in the second) is not specified.

What are your options:

In JavaScript, most of the time ordered lists are represented by arrays. You can us an array if like an ordered list in your case:

var values= ["key1","key2","key3"];
for(var i=0;i<values.length;i++){
   values[i];// order is guaranteed here.
}

Since order is guaranteed you can also easily reverse it:

var values= ["key1","key2","key3"].reverse();//reverse is an array method
for(var i=0;i<values.length;i++){
   values[i];// order is guaranteed here.
}

If you already have an object you can grab its keys and .sort them based on some criterion, for example - in a lexicographical order :

var obj = {"key1":"value1","key2":"value2","key3":"value3"};
var keys = Object.keys(obj).sort().reverse(); // see note
for(var i=0;i<keys.length;i++){
    console.log(keys[i]);
}
// will output key3, key2, key1

note: you can pass a comparator to .sort and have it reverse it for you instead of chaining - which is probably better practice. I kept it this way for pedagogical purposes.

Community
  • 1
  • 1
Benjamin Gruenbaum
  • 270,886
  • 87
  • 504
  • 504
  • I believe that is no longer the case. for...in now always iterates over integer keys between 0 and 2^32-1 first in order of low to high, then over the rest of the keys in the same order that they were added to the object. – PHP Guru Aug 30 '23 at 06:02
2

You could use var keys = Objects.keys(keyvalues).sort() to get all the keys of the object then just iterate through it backwords in a for(int = keys.length;i>0;i--) loop accessing the original object like so value = object[keys[i]] in the loop

EDIT: Added the sort function call to the array of keys returned by Object.keys.

Legion
  • 796
  • 7
  • 12
  • The order returned by `Object.keys` is not guaranteed. –  Jul 11 '13 at 18:13
  • How would you iterate backwards through an **object** that has dynamically changing keys then? – Legion Jul 11 '13 at 18:18
  • 1
    @Legion You can sort them - see my answer. The fact the keys iterated in the order key1,key2,key3 is completely coincidental - it's implementation detail and may change any day. – Benjamin Gruenbaum Jul 11 '13 at 18:22