共计 2546 个字符,预计需要花费 7 分钟才能阅读完成。
导读 | 正如我们所看到的,for-of 循环比 for、for-in 和 .forEach() 的可用性要好。 |
这篇文章比较了遍历数组的四种方式:
for 循环:
for (let index=0; index | |
for-in 循环: | |
for (const key in someArray) {console.log(key); | |
} | |
数组的 .forEach() 方法: | |
someArray.forEach((elem, index) => {console.log(elem, index); | |
}); | |
for-of 循环: | |
for (const elem of someArray) {console.log(elem); | |
} | |
for-of 往往是最好的选择,我们会知道为什么。 | |
for 循环[ES1] | |
JavaScript 中的普通 for 循环已经很老了,它在 ECMAScript 1 中就已经存在了。这个 `for` 循环记录了 arr 的每个元素的索引和值: | |
const arr = ['a', 'b', 'c']; | |
arr.prop = 'property value'; | |
for (let index=0; index | |
此循环的优缺点是什么? | |
它是相当通用的,但可惜的是,当我们要做的是在一个数组上循环时,它也很啰嗦。 | |
如果我们不想从第一个 Array 元素开始循环,它还是很有用的。其他的循环机制都不能让我们这样做。 | |
for-in 循环 [ES1] | |
for-in 循环和 for 循环一样古老 -- 它在 ECMAScript 1 中也已经存在。这个 for-in 循环记录了 arr 的键: | |
const arr = ['a', 'b', 'c']; | |
arr.prop = 'property value'; | |
for (const key in arr) {console.log(key); | |
} | |
// 输出: | |
// '0' | |
// '1' | |
// '2' | |
// 'prop' | |
for-in 不是循环遍历数组的好选择: | |
它访问属性键,而不是值。 | |
作为属性键,Array 元素的索引是字符串,而不是数字 | |
它访问所有可枚举的属性键(包括自有的和继承的),而不仅仅是 Array 元素的属性键。 | |
for-in 访问继承的属性确实有一个用例:循环一个对象的所有可枚举属性。 | |
Array 方法.forEach() [ES5] | |
考虑到 for 和 for-in 都不是特别适合在 Array 上循环,在 ECMAScript 5 中引入了一个辅助方法:Array.prototype.forEach()。 | |
const arr = ['a', 'b', 'c']; | |
arr.prop = 'property value'; | |
arr.forEach((elem, index) => {console.log(elem, index); | |
}); | |
// 输出: | |
// 'a', 0 | |
// 'b', 1 | |
// 'c', 2 | |
这个方法真的很方便。它让我们无需做太多事情就能访问 Array 元素和 Array 元素索引。箭头函数 (在 ES6 中引入) 使这种方法在语法上更加优雅。 | |
.forEach() 的主要缺点是: | |
你不能在这种循环的“主体”中使用 await。 | |
你不能提早退出 .forEach() 循环,在 for 循环中,我们可以使用 break。 | |
退出.forEach()-- 一个变通方法 | |
如果你想使用像 .forEach() 这样的循环并提前离开,有一个变通的办法:.some() 也会在所有 Array 元素上循环,如果它的回调返回一个真值,就会停止。 | |
onst arr = ['red', 'green', 'blue']; | |
arr.some((elem, index) => {if (index >= 2) {return true; // break from loop} | |
console.log(elem); | |
// This callback implicitly returns `undefined`, which | |
// is a falsy value. Therefore, looping continues. | |
}); | |
// 输出: | |
// 'red' | |
// 'green' | |
可以说,这是对 .some() 的滥用,我不知道这段代码有多容易理解(与 for-of 和 break 相比)。 | |
for-of 循环 [ES6] | |
在 ECMAScript 6 中,for-of 循环被添加到 JavaScript 中。 | |
const arr = ['a', 'b', 'c']; | |
arr.prop = 'property value'; | |
for (const elem of arr) {console.log(elem); | |
} | |
// 输出: | |
// 'a' | |
// 'b' | |
// 'c' | |
for-of 非常适合循环遍历数组: | |
它在数组元素上进行迭代。 | |
我们可以使用 await:而且,如果需要,可以轻松迁移到 for-await-of。 | |
我们可以使用 break 和 continue -- 即使是外部范围。 | |
for-of 和 iterable 对象 | |
for-of 的另一个好处是,我们不仅可以在 Arrays 上循环,还可以在任何可迭代对象上循环 -- 例如,在 Maps 上循环。 | |
const myMap = new Map() | |
.set(false, 'no') | |
.set(true, 'yes') | |
; | |
for (const [key, value] of myMap) {console.log(key, value); | |
} | |
// 输出: | |
// false, 'no' | |
// true, 'yes' | |
在 myMap 上迭代产生 [key, value] 对,我们对其进行解构,以直接访问每个对的组件。 | |
for-of 与 数组下标 | |
数组方法 .entries() 在 [index, value] 对上返回一个可迭代对象。如果使用 for-of 和解构这个方法,我们可以方便地访问 Array 索引。 | |
const arr = ['chocolate', 'vanilla', 'strawberry']; | |
for (const [index, elem] of arr.entries()) {console.log(index, elem); | |
} | |
// 输出: | |
// 0, 'chocolate' | |
// 1, 'vanilla' | |
// 2, 'strawberry' | |
总结 | |
正如我们所看到的,for-of 循环比 for、for-in 和 .forEach() 的可用性要好。 | |
四种循环机制之间的任何性能差异通常都不重要。如果有的话,你可能正在做一些计算量非常大的事情,切换到 WebAssembly 可能是有意义的。 | |
阿里云 2 核 2G 服务器 3M 带宽 61 元 1 年,有高配 | |
腾讯云新客低至 82 元 / 年,老客户 99 元 / 年 | |
代金券:在阿里云专用满减优惠券 | |
正文完
星哥玩云-微信公众号
