Javascript计数对象中的对象数 [英] Javascript counting number of objects in object
问题描述
我有一个对象:
Object {0 = Object,1 = Object,2 = Object}从console.log(obj.Data)输出;
但是没有办法计算对象中的对象数量,
控制台。 log(obj.Data [0] .length); //它不工作
console.log(obj.Data.length); //它不工作
这对我来说有点棘手。希望你们可以帮助。
这是最简单的方法,具有优良的性能和兼容性,旧的和新的浏览器,是包括 Lo-Dash 或下划线。
然后你可以使用 _。size(object)
或 _。keys(object).length
对于您的 obj.Data
,您可以使用以下方法进行测试:
console.log(_.size(obj.Data));
或:
code> console.log(_.keys(obj.Data).length);
Lo-Dash和Underscore都是优秀的库;你会发现一个在你的代码非常有用。 (它们彼此相似; Lo-Dash是具有一些优点的较新版本。)
或者,您可以在代码中包含此函数,通过对象的属性并计数它们:
function ObjectLength(object){
var length = 0;
for(var key in object){
if(object.hasOwnProperty(key)){
++ length;
}
}
返回长度;
};
您可以用以下方式测试:
console.log(ObjectLength(obj.Data));
但是代码不如现代浏览器那么快。对于一个在现代浏览器中速度更快,但仍在旧版本中使用的版本,您可以使用:
function ObjectLength_Modern
return Object.keys(object).length;
}
function ObjectLength_Legacy(object){
var length = 0;
for(var key in object){
if(object.hasOwnProperty(key)){
++ length;
}
}
返回长度;
}
var ObjectLength =
Object.keys? ObjectLength_Megen:ObjectLength_Legacy;
和之前一样,测试它:
console.log(ObjectLength(obj.Data));
此代码使用 Object.keys(object).length $ c
但是如果你要去所有这些工作,我建议使用Lo-
我设置了一个 jsPerf比较这些各种方法的速度。请在您可以随时添加到测试中的任何浏览器中运行。
感谢 Barmar 对较新的浏览器建议 Object.keys
在他的答案。
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.
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.
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.
Then you can use either _.size(object)
or _.keys(object).length
For your obj.Data
, you could test this with:
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.)
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).length
in modern browsers and falls back to counting in a loop for old browsers.
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.
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.
Thanks to Barmar for suggesting Object.keys
for newer browsers in his answer.
这篇关于Javascript计数对象中的对象数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!