views:

53

answers:

3

Hi,

I want to access the object in [] JSON literal as an array using FOR...IN. But iterating through FOR...IN gives the object x undefined. Please see the code below.

var myJSONObject = [
    {"ircEvent": "PRIVMSG", "method": "newURI", "regex": "^http://.*"},
    {"ircEvent": "PRIVMSG", "method": "deleteURI", "regex": "^delete.*"},
    {"ircEvent": "PRIVMSG", "method": "randomURI", "regex": "^random.*"}
];

for (var x in myJSONObject) {
    alert(x['method']);
}

You may test the code online here @ JSBin

Regards,
Munim

+1  A: 

Try

for (var x in myJSONObject) {
    alert( myJSONObject[x]['method'] );
}
Christopher W. Allen-Poole
This works! Thanks! Can you please explain? I see `x` as `undefined` and how does `myJSONObject[x]` works?
Munim Abdul
Don't use `for … in` to iterate over an array: https://developer.mozilla.org/en/JavaScript/Reference/Statements/for...in#Description
Marcel Korpel
`x` in this case, is an identifier, not the actual element. So, in your first iteration, `x` would be equal to `0`. However, as mentioned by Marcel, when iterating over arrays, you should be using a standard `for` loop.
Ryan Kinal
Everyone is right when they say, "for...in" is bad for a numerically indexed array. I simply copied the example you used and added the correction.
Christopher W. Allen-Poole
in for x in y, the x value represents a contained property name. In an array, that will mostly be a number, in a dynamic object it can be any type of primitive (string, number, boolean, etc). Since you are looking for the VALUES associated with those names, you look up the values through array notation: y[ x ]. The value myJSONObject[x] has a method property. myJSONObject does not.
Christopher W. Allen-Poole
A: 

The for .. in syntax gives you the keys of the iterated object, not the values. Thus, you have to do the following:

for (var idx in myJSONObject) {
    alert(myJSONObject[idx]['method']);
}
Mike Axiak
Don't use `for … in` to iterate over an array: https://developer.mozilla.org/en/JavaScript/Reference/Statements/for...in#Description
Marcel Korpel
+5  A: 

You shouldn't iterate over an array using for … in:

Although it may be tempting to use this as a way to iterate over an Array, this is a bad idea. The for...in statement iterates over user-defined properties in addition to the array elements, so if you modify the array's non-integer or non-positive properties (e.g. by adding a "foo" property to it or even by adding a method or property to Array.prototype), the for...in statement will return the name of your user-defined properties in addition to the numeric indexes. Also, because order of iteration is arbitrary, iterating over an array may not visit elements in numeric order. Thus it is better to use a traditional for loop with a numeric index when iterating over arrays.

Just loop through your array this way:

for (var i = 0, length = myJSONObject.length; i < length; i++) {
    alert(myJSONObject[i].method);
}

See JSBin.

Marcel Korpel