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
}
8
  • 3
    In an iterative loop, the integer i is the key... if you have a separate key property then your first example should work. Commented Aug 7, 2012 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. Commented Aug 7, 2012 at 23:05
  • are you using any libraries like jQuery at all? Commented Aug 7, 2012 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. Commented Aug 7, 2012 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) Commented Aug 10, 2012 at 23:53

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

5 Comments

Sure I can, as pointed out here. 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—in an array, the "index" is the key (i.e. numeric property) name.
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.
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.
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.
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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.