Count Key/values In Json
Possible Duplicate: Length of Javascript Associative Array I have a JSON that looks like this: Object: www.website1.com : 'dogs' www.website2.com : 'cats' >__proto__
Solution 1:
You have to count them yourself:
functioncount(obj) {
var count=0;
for(var prop in obj) {
if (obj.hasOwnProperty(prop)) {
++count;
}
}
return count;
}
Although now that I saw the first comment on the question, there is a much nicer answer on that page. One-liner, probably just as fast if not faster:
functioncount(obj) { returnObject.keys(obj).length; }
Be aware though, support for Object.keys()
doesn't seem cross-browser just yet.
Solution 2:
.length
only works on arrays, not objects.
var count = 0;
for(var key in json)
if(json.hasOwnProperty(key))
count++;
Post a Comment for "Count Key/values In Json"