在 Javascript / jQuery 中获取对象的长度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11851306/
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
Getting Length of Object in Javascript / jQuery
提问by Resin01
I am trying to set up an array in jQuery and I then need to do a for loop on it. But it seems that I cant use an associative array for some reason?
我正在尝试在 jQuery 中设置一个数组,然后我需要对其进行 for 循环。但似乎由于某种原因我不能使用关联数组?
var items = new Array();
items['foo'] = 123456;
items['bar'] = 789012;
items['baz'] = 345678;
items['bat'] = 901234;
alert(items.length);
This is just a test, but it return 0?
这只是一个测试,但它返回0?
回答by thecodeparadox
You can't make associative array
in JavaScript like what you want, instead you can use Object.
你不能associative array
像你想要的那样在 JavaScript 中制作,相反,你可以使用Object。
For example:
例如:
var items = {
foo : 123456,
bar : 789012,
baz : 345678,
bat : 901234
}
And to calculate the length you can do:
并计算您可以执行的长度:
var getObjectSize = function(obj) {
var len = 0, key;
for (key in obj) {
if (obj.hasOwnProperty(key)) len++;
}
return len;
};
Use: getObjectSize(items); // output: 4
利用: getObjectSize(items); // output: 4
For more see here.
有关更多信息,请参见此处。
Another one is:
另一种是:
Object.keys(items).length;
But not supported by all browsers.
但并非所有浏览器都支持。
回答by Blazemonger
Another approach might be to set up two different arrays, which you construct in parallel:
另一种方法可能是设置两个不同的数组,您可以并行构建它们:
var items = [], items2 = [];
items.push('foo');
items2.push(123456);
// etc.
alert(items2.length);?
The efficiency of this approach depends on how you'll use it. If you're only going to loop through the list of items and do something to each of them, this approach may be more efficient. But if you need to useit like an associative array (items['foo']
), then you're better off building an object.
这种方法的效率取决于您将如何使用它。如果您只想遍历项目列表并对每个项目做一些事情,这种方法可能更有效。但是,如果您需要像关联数组 ( )一样使用它items['foo']
,那么最好构建一个对象。
回答by jbabey
var items = new Array();
items['foo'] = 123456;
The problem lies in the very first line. You believe that you are adding an item to the array at the index foo
, but you are actually adding a property to the items
variable with a key foo
and value 123456
. If you were to type items.foo
it would give you back your 123456
.
问题出在第一行。您认为您是在索引处向数组中添加一个项目foo
,但实际上您正在向items
具有键foo
和值的变量添加一个属性123456
。如果您要输入,items.foo
它会将您的123456
.
The problem with this approach is that adding a property to an array does not magically increase it's length.
这种方法的问题在于,向数组添加属性并不会神奇地增加它的长度。
If you want to have non-numeric indexes, you need to use an object instead of an array:
如果您想拥有非数字索引,则需要使用对象而不是数组:
var items = {
foo: 123456,
bar: 789012,
baz: 345678,
bat: 901234
};
回答by Willem Mulder
The .length property returns the highest numerical index of the array. Thus, in your case, there is no numerical index and it returns 0. Try
.length 属性返回数组的最高数字索引。因此,在您的情况下,没有数字索引,它返回 0。尝试
items[98] = "something";
items.length will be 98..! Use the .length property with caution, and if you also want to count the non-numerical indici, loop over the Object (an Array is also an Object) and count its ownProperties.
items.length 将是 98..!请谨慎使用 .length 属性,如果您还想计算非数字索引,请循环遍历对象(数组也是对象)并计算其自己的属性。