0

Possible Duplicate:
For..In loops in javascript - key value pairs

I need to loop through an array in order, just like in this question. However, I also need to get access to the key name. How can I do that while in a numerically indexed loop?

for(i=0; i<arr.length; i++){
    alert(arr[i].key); // clearly won't work
}

but

for(key in arr){
    alert(arr[key]); // works, but it doesn't loop through in the right order
}
Community
  • 1
  • 1
brentonstrine
  • 21,694
  • 25
  • 74
  • 120
  • 3
    In an iterative loop, the integer `i` _is_ the key... if you have a separate key property then your first example should work. – TheZ Aug 07 '12 at 23:00
  • @Greg, Why is that a duplicate? I'm not asking how to use PHP style `as` at all. I'm asking how to loop in order while also getting a key. – brentonstrine Aug 07 '12 at 23:05
  • are you using any libraries like jQuery at all? – Eric Strom Aug 07 '12 at 23:07
  • @EricStrom, I'm working with some ajax returned JSON, and yes, I do have jQuery available. I can't think of how to make `.each()` work on JSON that way though, if that's what you're thinking. – brentonstrine Aug 07 '12 at 23:10
  • This is a little late, but if you return put the returned json into an array using array = JSON.parse, you could then go through each object using jquery's .each(key, value) – Eric Strom Aug 10 '12 at 23:53
  • @casperOne Not even any response at all as to how it's a duplicate? – brentonstrine Aug 11 '12 at 00:47
  • @brentonstrine the onus is on you to indicate why it's not a duplicate. – casperOne Aug 11 '12 at 01:18
  • @CasperOne Fair enough, and I did so already in my response to Greg. I just want at least an acknowledgement of that or some kind of response. – brentonstrine Aug 11 '12 at 06:57

2 Answers2

1

You cannot list through an JavaScript object's properties and expect them to be returned in a particular order.

Read this blog post and check out the part about for loop order.

Jonas G. Drange
  • 8,749
  • 2
  • 27
  • 38
  • Sure I can, as pointed out [here](http://stackoverflow.com/a/7276056/925897). None of the loops in your link are numerically indexed. I'm asking about numerically indexed loops (which go in order, as far as I know) and how to get the `key` form them. – brentonstrine Aug 07 '12 at 23:03
  • @brentonstrine—in an array, the "index" **is** the key (i.e. numeric property) name. – RobG Aug 07 '12 at 23:13
  • As stated above in @TheZ comment, i is the key. Sorry if I missed the point, but key indicates a property of an object, not an array. – Jonas G. Drange Aug 07 '12 at 23:15
  • Crap, I guess I was assuming that it was possible to loop through it numerically. I guess my real question is how to loop through an object (or associative array with keys) in order. – brentonstrine Aug 07 '12 at 23:23
  • In either case I hope you found what you were looking for. Chrome can let you do this (somewhat predictably) other browsers not so much. – Jonas G. Drange Aug 07 '12 at 23:26
0

If you want to "sort" an object, then you can put the key names in an array, sort that, then access the object properties in the order they are in the array, e.g.:

var obj = { '3':3, '0':0, '2':2, '1':1};
var props = [];

for (var p in obj) {

  if (obj.hasOwnProperty(p)) {
    props.push(p);
  }
}

props.sort();

for (var i=0, iLen=props.length; i<iLen; i++0 {
  alert(obj[props[i]]); // 0, 1, 2, 3
}

In regard to the order of object properties, ES5 says:

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

http://es5.github.com/#x12.6.4

You may discover in the comments of a popular library the statement "Own properties are enumerated firstly", which is contradicted by the behaviour of at least one widely used browser and therefore should be disregarded.

Community
  • 1
  • 1
RobG
  • 142,382
  • 31
  • 172
  • 209