Javascript 如何获取对象长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5533192/
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
How to get object length
提问by Larry Cinnabar
Is there any built-in function that can return the length of an object?
是否有任何可以返回对象长度的内置函数?
For example, I have a = { 'a':1,'b':2,'c':3 }
which should return 3
. If I use a.length
it returns undefined
.
例如,我有a = { 'a':1,'b':2,'c':3 }
which 应该返回3
. 如果我使用a.length
它返回undefined
.
It could be a simple loop function, but I'd like to know if there's a built-in function?
它可能是一个简单的循环函数,但我想知道是否有内置函数?
There is a related question (Length of a JSON object) - in the chosen answer the user advises to transform object into an array, which is not pretty comfortable for my task.
有一个相关的问题(JSON 对象的长度) - 在选择的答案中,用户建议将对象转换为数组,这对我的任务来说不太舒服。
回答by David Tang
For browsers supporting Object.keys()you can simply do:
对于支持Object.keys() 的浏览器,您可以简单地执行以下操作:
Object.keys(a).length;
Otherwise (notably in IE < 9), you can loop through the object yourself with a for (x in y)
loop:
否则(特别是在 IE < 9 中),您可以自己使用循环遍历对象for (x in y)
:
var count = 0;
var i;
for (i in a) {
if (a.hasOwnProperty(i)) {
count++;
}
}
The hasOwnProperty
is there to make sure that you're only counting properties from the object literal, and not properties it "inherits" from its prototype.
这hasOwnProperty
是为了确保您只计算对象文字中的属性,而不是它从其原型“继承”的属性。
回答by ?ime Vidas
This should do it:
这应该这样做:
Object.keys(a).length
However, Object.keys
is not supported in IE8 and below, Opera and FF 3.6 and below.
但是,Object.keys
在 IE8 及以下、Opera 和 FF 3.6 及以下不支持。
Live demo:http://jsfiddle.net/simevidas/nN84h/
回答by Fazle Rabbi
回答by Philip Schweiger
Have you taken a look at underscore.js(http://underscorejs.org/docs/underscore.html)? It's a utility library with a lot of useful methods. There is a collection size
method, as well as a toArray method, which may get you what you need.
你看过underscore.js( http://underscorejs.org/docs/underscore.html) 吗?它是一个实用程序库,有很多有用的方法。有一个集合size
方法,以及一个 toArray 方法,它可以满足您的需求。
_.size({one : 1, two : 2, three : 3});
=> 3
回答by tibalt
Summarizing all together, here is a universal function (including ie8 support):
总结一下,这里有一个通用功能(包括ie8支持):
var objSize = function(obj) {
var count = 0;
if (typeof obj == "object") {
if (Object.keys) {
count = Object.keys(obj).length;
} else if (window._) {
count = _.keys(obj).length;
} else if (window.$) {
count = $.map(obj, function() { return 1; }).length;
} else {
for (var key in obj) if (obj.hasOwnProperty(key)) count++;
}
}
return count;
};
document.write(objSize({ a: 1, b: 2, c: 3 }));
// 3
回答by Larry Cinnabar
In jQuery i've made it in a such way:
在 jQuery 中,我是这样实现的:
len = function(obj) {
var L=0;
$.each(obj, function(i, elem) {
L++;
});
return L;
}
回答by Robert Brisita
So one does not have to find and replace the Object.keys method, another approach would be this code early in the execution of the script:
因此不必查找和替换 Object.keys 方法,另一种方法是在脚本执行的早期使用以下代码:
if(!Object.keys)
{
Object.keys = function(obj)
{
return $.map(obj, function(v, k)
{
return k;
});
};
}
回答by Andrew Plank
Here's a jQuery-ised function of Innuendo's answer, ready for use.
这是 Innuendo 答案的 jQuery 化函数,可供使用。
$.extend({
keyCount : function(o) {
if(typeof o == "object") {
var i, count = 0;
for(i in o) {
if(o.hasOwnProperty(i)) {
count++;
}
}
return count;
} else {
return false;
}
}
});
Can be called like this:
可以这样调用:
var cnt = $.keyCount({"foo" : "bar"}); //cnt = 1;
回答by Mahendra Kulkarni
One more answer:
再来一个答案:
var j = '[{"uid":"1","name":"Bingo Boy", "profile_img":"funtimes.jpg"},{"uid":"2","name":"Johnny Apples", "profile_img":"badtime.jpg"}]';
obj = Object.keys(j).length;
console.log(obj)
回答by Jpsy
For those coming here to find the item count of something that is already a jQuery object:
.length is what you are looking for:
对于那些来这里查找已经是jQuery 对象的东西的项目计数的人:
.length 是您要查找的内容:
Example:
例子:
len = $('#divID').length;
alert(len);