这篇文章比较了遍历数组的四种方式:
创新互联专业为企业提供中原网站建设、中原做网站、中原网站设计、中原网站制作等企业网站建设、网页设计与制作、中原企业网站模板建站服务,十载中原做网站经验,不只是建网站,更提供有价值的思路和整体网络服务。
for 循环:
- for (let index=0; index < someArray.length; index++) {
- const elem = someArray[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 往往是最好的选择,我们会知道为什么。
JavaScript中的普通 for 循环已经很老了,它在ECMAScript 1中就已经存在了。这个 `for` 循环记录了arr的每个元素的索引和值:
- const arr = ['a', 'b', 'c'];
- arr.prop = 'property value';
- for (let index=0; index < arr.length; index++) {
- const elem = arr[index];
- console.log(index, elem);
- }
- // 输出:
- // 0, 'a'
- // 1, 'b'
- // 2, 'c
此循环的优缺点是什么?
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 不是循环遍历数组的好选择:
for-in 访问继承的属性确实有一个用例:循环一个对象的所有可枚举属性。
考虑到 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() 的主要缺点是:
如果你想使用像 .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 相比)。
在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 非常适合循环遍历数组:
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] 对,我们对其进行解构,以直接访问每个对的组件。
数组方法 .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可能是有意义的。
文章标题:遍历数组:for、for-in、forEach、for-of
文章起源:http://www.csdahua.cn/qtweb/news22/86472.html
网站建设、网络推广公司-快上网,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等
声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 快上网