javascript Javascript计算对象中对象的数量

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16976904/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 06:38:44  来源:igfitidea点击:

Javascript counting number of objects in object

javascriptobjectcount

提问by Bryan Learn

I have an object something like:

我有一个类似的对象:

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

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.

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

I have tried

我努力了

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.

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

回答by Michael Geary

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

最简单的方法是在您的页面中包含Lo-DashUnderscore,具有出色的性能和与新旧浏览器的兼容性。

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

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

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

对于您的obj.Data,您可以使用以下方法进行测试:

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

or:

或者:

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

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.)

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

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;

and as before, test it with:

和以前一样,测试它:

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

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

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

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.

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

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.

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

Thanks to Barmarfor suggesting Object.keysfor newer browsersin his answer.

由于Barmar建议Object.keys为新的浏览器在他的回答。

回答by Barmar

In recent browsers you can use:

在最近的浏览器中,您可以使用:

Object.keys(obj.Data).length

See MDN

MDN

For older browsers, use the for-inloop in Michael Geary's answer.

对于较旧的浏览器,请使用for-inMichael Geary 的回答中的循环。

回答by Sudarshan

Try Demo Here

在这里尝试演示

var list ={}; var count= Object.keys(list).length;