开发者

Length of an object filled with objects?

开发者 https://www.devze.com 2023-02-22 05:15 出处:网络
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.

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.

Length of an object filled with objects?


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
0

精彩评论

暂无评论...
验证码 换一张
取 消