Satya
Satya

Reputation:

json result returns error

I have a json array

var states = { "1": "California", "2": "LA" };

How can i count and print the items in states. It should print 2.

if I use states.length it gives an error

Upvotes: 1

Views: 153

Answers (2)

Alex Ciminian
Alex Ciminian

Reputation: 11498

If you need to count the members of an object you can do this:

for (val in states) {
    alert(states[val]);
    count++;
}
alert(count);

Upvotes: 2

Daniel A. White
Daniel A. White

Reputation: 190986

Thats not an array.

var states = [ "California", "LA" ];

Upvotes: 1

Related Questions