How to count JavaScript array objects?
You can try this code, it works perfectly in a browser:
Object.keys(member).length;
That's not an array, is an object literal, you should iterate over the own properties of the object and count them, e.g.:
function objectLength(obj) {
var result = 0;
for(var prop in obj) {
if (obj.hasOwnProperty(prop)) {
// or Object.prototype.hasOwnProperty.call(obj, prop)
result++;
}
}
return result;
}
objectLength(member); // for your example, 3
The hasOwnProperty
method should be used to avoid iterating over inherited properties, e.g.
var obj = {};
typeof obj.toString; // "function"
obj.hasOwnProperty('toString'); // false, since it's inherited