Javascript 迭代对象数组javascript - 奇怪的行为?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6565281/
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
Iterating over array of objects javascript - odd behaviour?
提问by Andy Hin
var myArr = [{a:1, b:2}, {c:3, d:4}];
for (var item in myArr) {
console.log(item);
}
Item returns the key (ex: 0, 1) instead of the object itself. Why?
Item 返回键(例如:0, 1)而不是对象本身。为什么?
回答by sgokhales
Douglas Crockford recommends in JavaScript: The Good Partsto avoid using the for in
statement.
Douglas Crockford 在JavaScript: The Good Parts 中建议避免使用该for in
语句。
If you use for in
to loop over property names in an object, the results are not ordered.
如果您使用for in
循环遍历对象中的属性名称,则结果不会排序。
The for in
loop is best for iterating over name-valuepairs, and the for each
loop best for iterating over values i.e arrays.
该for in
循环是最好的用于遍历名称-值对,并且for each
在值循环最适合迭代即阵列。
E.g,
例如,
var o = {'name':'Batman', 'age':33, 'city':'Gotham City'};
for (var p in o) {
console.log(p+': '+o[p]);
}
There's no way we can get the property name if we were to use the For Each Loop for the above object.
如果我们要对上述对象使用 For Each 循环,则无法获得属性名称。
Note :
笔记 :
- The For inLoop is best for name-value pairs.
- The For EachLoop is best for iterable values. Eg: arrays, and objects if you are not interested in the name of the property.
- 在对于循环是最好的名称-值对。
- 在对于每对迭代值循环是最好的。例如:数组和对象,如果您对属性的名称不感兴趣。
回答by pomeh
Javascript for
..in
loops always return the index/name, not the value. To get what you want, you should use:
Javascript for
..in
循环总是返回索引/名称,而不是值。为了得到你想要的,你应该使用:
var myArr = [{a:1, b:2}, {c:3, d:4}];
for (var index in myArr) {
console.log( myArr[index] );
}
However, as said before, the for
..in
statement should be use with caution, and it is not intended to be used with an array. You should use a for
loop instead
但是,如前所述,for
..in
语句应谨慎使用,不应与数组一起使用。你应该使用for
循环代替
var myArr = [{a:1, b:2}, {c:3, d:4}];
for( var i=0, l=myArr.length; i<l; i++ ) {
console.log( myArr[i] );
}
回答by SLaks
The for
... in
loop iterates over the keys (properties) of an object.
It is not intended to be used for arrays.
所述for
...in
循环遍历对象的键(属性)。
它不打算用于数组。
It will also iterate over properties inherited from the object's prototype
.
它还将迭代从对象的prototype
.
回答by GrayedFox
Adding an answer here to accommodate the latest ECMAScript 6 standard.
在此处添加答案以适应最新的 ECMAScript 6 标准。
Check out thispage for a great read with examples.
查看此页面以阅读示例。
And a rather tasty caveat: this awesome new functionality will work with nearly ever iterable object! From MDN:
还有一个相当不错的警告:这个很棒的新功能几乎适用于任何可迭代对象!来自 MDN:
The for...of statement creates a loop iterating over iterable objects (including Array, Map, Set, String, TypedArray, arguments object and so on)...
for...of 语句创建一个循环迭代可迭代对象(包括 Array、Map、Set、String、TypedArray、arguments 对象等)...
So for example, you could use:
例如,您可以使用:
for (let item of myArr) {
console.log(item);
}
Although to super clear that you are logging an object, I would be a bit nicer to the next person to read your code by renaming "item" to "obj", producing this:
虽然要超级清楚您正在记录一个对象,但我会通过将“item”重命名为“obj”来更好地让下一个阅读您的代码的人,产生这个:
for (let obj of myArr) {
console.log(obj);
}
Why rename the variable? Well, although we use the term 'item' to denote any generic item in an array, your array only containsobjects. If you know, or expect, this array to only every contain objects, you could name the variable based on the typeof item (i.e. an object) the array contains.
为什么要重命名变量?好吧,虽然我们使用术语“项目”来表示数组中的任何通用项目,但您的数组仅包含对象。如果您知道或期望此数组仅包含每个对象,则可以根据数组包含的项目(即对象)的类型命名变量。
Happy coding!
快乐编码!