I've got an object filled with objects and I want to know how 开发者_如何转开发many items are in it. How can I do this with JS? Also running jQuery in the page.
Try this:
function countProperties(obj) {
var count = 0;
for (var prop in obj) {
if (obj.hasOwnProperty(prop))
++count;
}
return count;
}
See also: Number of elements in a javascript object
For ECMAScript 5-compatible agents, for example Chrome, try this:
var obj = {a:1, b:2, c:3};
console.log('Your object has ' + Object.keys(obj).length + ' elements in it');
// Your object has 3 elements in it
See also here.
Cheers
"object filled with objects" is more commonly known as an array. Anyway:
yourObject.length
精彩评论