简体   繁体   English

具有索引位置的 For..in 循环数组迭代

[英]For..in Loop Array Iteration with Index Position

I tried looping over an array with for..in loop and show the index of each item but it showed an error message.我尝试使用 for..in 循环遍历数组并显示每个项目的索引,但它显示了一条错误消息。 Who has an idea of how this thing work?谁知道这个东西是如何工作的?

For arrays you will need to use for...of rather than for...in which is for iterating over objects.对于数组,您需要使用for...of而不是for...in用于迭代对象。

However, for...of will not give you the index - only the value .但是, for...of不会给你索引- 只有value

Use forEach for this purpose:为此目的使用forEach

array.forEach(function (value, index) {

});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM