views:

1077

answers:

1

Hi,

I need to create some sort of a state for a bunch of elements on a page. The stats can be 1 or -1.

Now on the server side I will generate a JSON array and put it in my .aspx page like this:

var someArray = { 100:-1, 1001:1, 102:1, 103:-1 }

How do I loop through each value now in javascript?

BTW, is my JSON array format correct?

+4  A: 

Note that someArray is a misnomer as it is actually an Object. To loop through it, though:

for(key in someArray) {
    alert(someArray[key]);
}

As far as whether it is valid, the above works for me but I believe technically keys should be strings:

{
    "100": -1,
    "1001": 1,
    "102": 1,
    "103": -1 
}

Check out this handy JSON validator.

Paolo Bergantino
+1 for the JSON validator link.
William Brendel
btw, why is someArray.length now working?
mrblah
now working? It shouldn't be working. You're confusing arrays with objects, which are not the same thing.
Paolo Bergantino