9

Taking a JavaScript object with 4 properties:

function Object() {
  this.prop1;
  this.prop2;
  this.prop3;
  this.prop4;
}

var obj = new Object();

I use a for(in) loop to inspect each property since I don't know the number or name of the properties:

for(property in obj) {
  var prop = obj[property];
}

However I would like to process the properties starting with the last (prop4 in this example). I suppose I would like a reverse-for-in-loop.

How can I do this?

Thanks, Jack

Adding: The object I am referring to is the one returned from JSON.parse. The properties seem to be consistently ordered. There is no keys() method.

4
  • 5
    Objects do not have any order. The premise of your question is invalid. Commented Mar 18, 2012 at 21:25
  • Are you actually calling your own object 'Object'? That can cause problems, as every new object in the system will try to inherit from it. Commented Mar 18, 2012 at 21:25
  • Obviously order matters to you, using an object with properties is the wrong tool for this job. Commented Mar 18, 2012 at 21:27
  • The object I am referring to is the one returned from JSON.parse. The properties seem to be consistently ordered. Commented Mar 19, 2012 at 19:24

3 Answers 3

14

A for (x in y) does not process the properties in any specific order so you cannot count on any desired order.

If you need to process the properties in a specific order, you will need to get all the properties into an array, sort the array appropriately and then use those keys in the desired order.

Using ES5 (or an ES5 shim), you can get all properties into an array with:

var keys = Object.keys(obj);

You could then sort them either in standard lexical order or sort with your own custom function:

keys.sort(fn);

And, then you could access them in your desired order:

for (var i = 0; i < keys.length; i++) {
    // process obj[keys[i]]
}
Sign up to request clarification or add additional context in comments.

7 Comments

And you should probably use the Object.keys method, or at least check that the property is not "inherited" with obj.hasOwnProperty(prop)
The object I am referring to is the one returned from JSON.parse. The properties seem to be consistently ordered. There is no keys() method.
By language definition, properties may be returned in any order. The .keys() method is part of the ECMAScript 5 specification and is a method on the Object prototype (so all Objects will have it). All modern browsers implement it and you can read about it here. It gives you all the enumerable property names on an object in an array. If you want compatibility with older browsers that don't implement it, then it's easy to add a shim that will add the .keys() method if it doesn't already exist.
I am using Chrome 17.0 (a modern browser) and there is no keys() method on the object returned from JSON.parse or any of the objects stored in that object. There error: Uncaught TypeError: Object #<Object> has no method 'keys' (anonymous function)
OK. So I decided to RTFM, I was assuming(!) myObject.keys(). I didn't realise it was a static method on Object.
|
2

The ECMAScript standard does not define an order to iteration for for in loops. You will want an array, if your datatypes are to be sorted.

Comments

2

Arrays are ordered objects. Properties in objects are inherently unordered. However, if you have some specific reason that you want to work from back to front of whatever the for..in construct would have produced, you could do:

var arr = [];
for (prop in obj) {
   arr.push(prop);
}

for (var n=arr.length; n--; ){
   var prop = obj[arr[n]];
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.