javascript - how to get size or count the number of properties of an object? -
this question has answer here:
is there method count number of properties (or size) of object (don't want use loop) ?
suppose have object obj as,
obj={id:'0a12',name:'nishant',phone:'mobile'};
then there method results 3 in case ?
object.keys
returns array containing names of object's own enumerable properties, so:
var count = object.keys(obj).length;
note there may loop involved (within object.keys
), @ least it's within javascript engine. object.keys
added es5, older browsers may not have (it can "shimmed," though; search "es5 shim" options).
note that's not quite same list of properties for-in
iterates, for-in
includes properties inherited prototype.
i don't believe there's way list of object's non-enumerable properties (that point of them!).
Comments
Post a Comment