javascript for ... in loop with string array 输出索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7480020/
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-26 00:15:21  来源:igfitidea点击:

for ... in loop with string array outputs indices

javascript

提问by PolandSpring

When I write some javascript such as this:

当我写一些像这样的javascript时:

var words = ['word1', 'word2', 'word3']

for (word in words) {
    console.log(word)
}

The resulting output is the numeric indices of the respective word. I did some searching on Google and I couldn't find the exact reason for this behavior. I'm guessing that this is completely expected behavior but I would like to know the reason why.

结果输出是相应单词的数字索引。我在 Google 上进行了一些搜索,但找不到这种行为的确切原因。我猜这完全是预期的行为,但我想知道原因。

Thanks!

谢谢!

回答by jfriend00

Why is nobody providing the correct answer? You NEVER iterate arrays with a for/inloop - that is only for iterating plain objects and their keys, not for iterating the items in an array.

为什么没有人提供正确答案?您永远不要使用循环来迭代数组for/in——这仅用于迭代普通对象及其键,而不是用于迭代数组中的项目。

You iterate arrays for a for loop like this:

您为 for 循环迭代数组,如下所示:

var words = ['word1', 'word2', 'word3'];

for (var i = 0, len = words.length; i < len; i++) {
    // here words[i] is the array element
}

Or, in a modern browser, you can use the .forEach()method of arrays:

或者,在现代浏览器中,您可以使用.forEach()数组的方法:

var words = ['word1', 'word2', 'word3'];

words.forEach(function(value, index, array) {
    // here value is the array element being iterated
});

See here at MDNfor more info on .forEach().

有关更多信息,请参见MDN 此处.forEach()

ndp's reference to this postshows some good examples of things that can go wrong using for/inwith arrays. You can make it works sometimes, but it is not the smart way to write Javascript array iteration.

ndp 对这篇文章的引用显示了一些很好的例子,说明使用for/in数组可能会出错。有时你可以让它工作,但这不是编写 Javascript 数组迭代的聪明方法。



Or, in more modern times, you can use the ES6 for/ofconstruct which is specially built for iterating an iterable (an array is an iterable):

或者,在更现代的时代,您可以使用for/of专为迭代可迭代对象(数组是可迭代对象)而构建的 ES6构造:

var words = ['word1', 'word2', 'word3'];

for (const w of words) {
    console.log(w);
}

Or, if you want both the value and the index, you can do this:

或者,如果你想要值和索引,你可以这样做:

var words = ['word1', 'word2', 'word3'];

for (const [index, w] of words.entries()) {
    console.log(index, ": ", w);
}

There are several advantages of for/ofover .forEach(). To start with, you have more loop control as you can use break, continue, returnto control the loop flow which .forEach()does not give you. In addition, there's no additional overhead for a callback function so, in some environments, it can be faster.

for/ofover有几个优点.forEach()。首先,你必须为你可以使用更多的回路控制breakcontinuereturn以控制其循环流动.forEach()不给你。此外,回调函数没有额外的开销,因此在某些环境中,它可以更快。

回答by Sriram

  1. for..in is used to iterate over the properties of a javascript object.
  2. Think of an array as a javascript object with indexes as properties.
  1. for..in 用于迭代 javascript 对象的属性。
  2. 将数组视为具有索引作为属性的 javascript 对象。

回答by ndp

For ... inis intended for objects-- see this question. Apparently it can (and is) used for arrays, but this has a few risks you may not want.

For ... in用于对象- 请参阅此问题。显然它可以(并且正在)用于数组,但这有一些您可能不想要的风险。

回答by JohnnyHK

As the other answers correctly point out, for...inis not for iterating over arrays. A better option is now available in newer JavaScript engines and transpilers like TypeScript with for...of:

正如其他答案正确指出的那样,for...in不是用于迭代数组。现在在较新的 JavaScript 引擎和转译器(如 TypeScript)中提供了一个更好的选择for...of

var words = ['word1', 'word2', 'word3']

for (word of words) {
    console.log(word)
}