How do I know how many items an object literal contains?
For example, how do I know that the myObject object liter开发者_如何学编程al contains two items?
var myObject = {
item1 : "blablabla",
item2 : "blablabla
};
How do I count a JavaScript object's attributes?
var n = 0
for (var i in o) ++n
alert(n)
If you didn't override the prototypes of myObject
,
var count = 0;
for (var k in myObject) ++ count;
return count;
Otherwise, see the answer haim's link.
精彩评论