Javascript计算对象中对象的数量 [英] Javascript counting number of objects in object

查看:51
本文介绍了Javascript计算对象中对象的数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的对象:

Object {0=Object, 1=Object, 2=Object} // Output from console.log(obj.Data);

但是没有办法统计object里面的对象个数,然后最后从子对象中获取属性值.

But there is no way that I can count the number of objects in object, then finally get the attribute value from the sub objects.

我试过了

console.log(obj.Data[0].length); // It does not work

console.log(obj.Data.length); // It does not work

这对我来说有点棘手.希望大家能帮忙.

This is a bit tricky for me. Hope you guys can help.

推荐答案

最简单的方法是包含 Lo-Dash下划线.

The easiest way to do this, with excellent performance and compatibility with both old and new browsers, is to include either Lo-Dash or Underscore in your page.

然后你可以使用 _.size(object)_.keys(object).length

Then you can use either _.size(object) or _.keys(object).length

对于你的 obj.Data,你可以用:

For your obj.Data, you could test this with:

console.log( _.size(obj.Data) );

或:

console.log( _.keys(obj.Data).length );

Lo-Dash 和 Underscore 都是优秀的库;您会发现其中任何一个在您的代码中都非常有用.(它们彼此相当相似;Lo-Dash 是具有一些优点的较新版本.)

Lo-Dash and Underscore are both excellent libraries; you would find either one very useful in your code. (They are rather similar to each other; Lo-Dash is a newer version with some advantanges.)

或者,您可以在代码中包含此函数,它只是循环遍历对象的属性并对其进行计数:

Alternatively, you could include this function in your code, which simply loops through the object's properties and counts them:

function ObjectLength( object ) {
    var length = 0;
    for( var key in object ) {
        if( object.hasOwnProperty(key) ) {
            ++length;
        }
    }
    return length;
};

您可以使用以下方法进行测试:

You can test this with:

console.log( ObjectLength(obj.Data) );

尽管如此,该代码的速度不如现代浏览器中的快.对于在现代浏览器中速度更快但在旧浏览器中仍然有效的版本,您可以使用:

That code is not as fast as it could be in modern browsers, though. For a version that's much faster in modern browsers and still works in old ones, you can use:

function ObjectLength_Modern( object ) {
    return Object.keys(object).length;
}

function ObjectLength_Legacy( object ) {
    var length = 0;
    for( var key in object ) {
        if( object.hasOwnProperty(key) ) {
            ++length;
        }
    }
    return length;
}

var ObjectLength =
    Object.keys ? ObjectLength_Modern : ObjectLength_Legacy;

和以前一样,用:

console.log( ObjectLength(obj.Data) );

此代码在现代浏览器中使用 Object.keys(object).length 并在旧浏览器中回退到循环计数.

This code uses Object.keys(object).length in modern browsers and falls back to counting in a loop for old browsers.

但如果您要完成所有这些工作,我建议您改用 Lo-Dash 或 Underscore,并获得这些库提供的所有好处.

But if you're going to all this work, I would recommend using Lo-Dash or Underscore instead and get all the benefits those libraries offer.

我设置了一个 jsPerf 来比较这些不同方法的速度.请在您可以添加到测试中的任何浏览器中运行它.

I set up a jsPerf that compares the speed of these various approaches. Please run it in any browsers you have handy to add to the tests.

感谢 Barmar 在他的回答中建议 Object.keys 用于较新的浏览器.

Thanks to Barmar for suggesting Object.keys for newer browsers in his answer.

这篇关于Javascript计算对象中对象的数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆