我们可以使用 for-of 循环访问数组元素:
for (const j of [1, 2, 3, 4, 5]) {
console.log(j);
}
如何修改此代码以访问当前索引?我想使用for-of语法实现,而不是forEach或for-in。
我们可以使用 for-of 循环访问数组元素:
for (const j of [1, 2, 3, 4, 5]) {
console.log(j);
}
如何修改此代码以访问当前索引?我想使用for-of语法实现,而不是forEach或for-in。
Array.prototype.keys
:
for (const index of ["a", "b", "c", "d", "e"].keys()) {
console.log(index);
}
Array.prototype.entries()
与 解构:
for (const [index, value] of ["a", "b", "c", "d", "e"].entries()) {
console.log(index, value);
}
Array#entries
返回索引和值,如果您都需要:
for (let [index, value] of array.entries()) {
}
document.styleSheets[0].cssRules.entries()
或者 document.styleSheets.entries()
以及其他许多可迭代的 DOM 结构都会抛出错误。仍然需要使用 lodash
中的 _.forEach()
。 - Steven Pribilinskiyfor (var value of document.styleSheets) {}
。如果您确实需要索引,您可以先通过 Array.from
将值转换为数组:for (let [index, value] of Array.from(document.styleSheets)) {}
。 - Felix KlingArray.from
就是胜利。 - Steven Pribilinskiy在这个充满新奇本地函数的世界里,我们有时会忘记基础。
for (let i = 0; i < arr.length; i++) {
console.log('index:', i, 'element:', arr[i]);
}
清晰、高效,而且你仍可以使用break
语句来终止循环。更棒的是,你还可以通过i--
从后往前开始遍历!
额外提示:如果在循环中频繁使用某个值,你可能希望在循环顶部使用const value = arr[i];
来获得一个易于阅读的引用。
for-of
循环,而且for (let [index, value] of array.entries())
要容易阅读得多。倒序遍历只需添加.reverse()
即可。 - user2493235for
循环比使用 for of array.entries()
快约8倍。https://jsbench.me/6dkh13vqrr/1 - Henrique Brunofor...of
循环的时候才会出现这种情况)。这个问题存在是有原因的... - lawrence-wittfor..of
循环中,我们可以通过使用array.entries()
来实现。 array.entries
返回一个新的数组迭代器对象。迭代器对象知道如何一次访问可迭代对象中的项目,同时跟踪其在该序列中的当前位置。next()
方法时,会生成键值对。在这些键值对中,数组索引是键,而数组项是值。
let arr = ['a', 'b', 'c'];
let iterator = arr.entries();
console.log(iterator.next().value); // [0, 'a']
console.log(iterator.next().value); // [1, 'b']
for..of
循环基本上是一种结构,它消耗一个可迭代对象并遍历所有元素(在内部使用迭代器)。我们可以将其与 array.entries()
结合使用,实现以下方式:
array = ['a', 'b', 'c'];
for (let indexValue of array.entries()) {
console.log(indexValue);
}
// we can use array destructuring to conveniently
// store the index and value in variables
for (let [index, value] of array.entries()) {
console.log(index, value);
}
如果您需要索引(index),您也可以自己处理它,但如果您需要键(key),这种方法无效。
let i = 0;
for (const item of iterableItems) {
// do something with index
console.log(i);
i++;
}
另一个方法可以使用Array.prototype.forEach()
作为
Array.from({
length: 5
}, () => Math.floor(Math.random() * 5)).forEach((val, index) => {
console.log(val, index)
})
for(let [index, element] of document.querySelectorAll('div').entries()) {
element.innerHTML = '#' + index
}
entries
方法。 - matanster在循环之前创建一个变量并赋一个整数值。
let index = 0;
然后在循环范围内使用加法赋值运算符
index += 1;
就这样,查看下面的代码片段示例。
let index = 0;
for (const j of [1, 2, 3, 4, 5]) {
console.log('index ',index);
index += 1;
}
你可以尝试在for...循环中使用indexOf方法
let arrData = [15, 64, 78]
for (const data of arrData) {
console.log("data value", data, "index of data ", arrData.indexOf(data));
}
O(n^2)
的复杂度 :( - Jiří对于那些使用的不是Array
或类似数组的对象,你可以轻松地构建自己的可迭代对象,这样你仍然可以像处理length
属性的localStorage
一样使用for of
语句:
function indexerator(length) {
var output = new Object();
var index = 0;
output[Symbol.iterator] = function() {
return {next:function() {
return (index < length) ? {value:index++} : {done:true};
}};
};
return output;
}
for (let index of indexerator(localStorage.length))
console.log(localStorage.key(index))
.entries()
和for-of
循环,结果比起使用.forEach()
循环慢了两倍。 https://jsperf.com/for-of-vs-foreach-with-index - user6269864.entries()
的for-of循环有什么问题吗? - Michał Perłakowski