How to get object length [duplicate] How to get object length [duplicate] json json

How to get object length [duplicate]

For browsers supporting Object.keys() you can simply do:


Otherwise (notably in IE < 9), you can loop through the object yourself with a for (x in y) loop:

var count = 0;var i;for (i in a) {    if (a.hasOwnProperty(i)) {        count++;    }}

The hasOwnProperty is there to make sure that you're only counting properties from the object literal, and not properties it "inherits" from its prototype.

This should do it:


However, Object.keys is not supported in IE8 and below, Opera and FF 3.6 and below.

Live demo:

Can be done easily with $.map():

var len = $.map(a, function(n, i) { return i; }).length;