2

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?

1 Answer 1

6

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.

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

1 Comment

now working? It shouldn't be working. You're confusing arrays with objects, which are not the same thing.

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.