译者:前端小智
做者:Ashish Lahoti
来源:odingnconcepts
点赞再看,微信搜索
【大迁世界】关注这个没有大厂背景,但有着一股向上积极心态人。本文
GitHub
https://github.com/qq44924588... 上已经收录,文章的已分类,也整理了不少个人文档,和教程资料。**
双12阿里服务器27
块,通用点击这里购买能够找我返现30,等于27
块就能买到了,只限新用户,能够用家人的手机号购买!javascript
今天咱们来看一下 Array中 Array.forEach()
和 Array.map()
方法之间的区别。前端
forEach()
和map()
方法一般用于遍历Array元素,但几乎没有区别,咱们来一一介绍。java
forEach()
方法返回undefined
,而map()
返回一个包含已转换元素的新数组。git
const numbers = [1, 2, 3, 4, 5]; // 使用 forEach() const squareUsingForEach = []; numbers.forEach(x => squareUsingForEach.push(x*x)); // 使用 map() const squareUsingMap = numbers.map(x => x*x); console.log(squareUsingForEach); // [1, 4, 9, 16, 25] console.log(squareUsingMap); // [1, 4, 9, 16, 25]
因为forEach()
返回undefined
,因此咱们须要传递一个空数组来建立一个新的转换后的数组。map()
方法不存在这样的问题,它直接返回新的转换后的数组。在这种状况下,建议使用map()
方法。github
map()
方法输出能够与其余方法(如reduce()
、sort()
、filter()
)连接在一块儿,以便在一条语句中执行多个操做。面试
另外一方面,forEach()
是一个终端方法,这意味着它不能与其余方法连接,由于它返回undefined
。数组
咱们使用如下两种方法找出数组中每一个元素的平方和:服务器
onst numbers = [1, 2, 3, 4, 5]; // 使用 forEach() const squareUsingForEach = [] let sumOfSquareUsingForEach = 0; numbers.forEach(x => squareUsingForEach.push(x*x)); squareUsingForEach.forEach(square => sumOfSquareUsingForEach += square); // 使用 map() const sumOfSquareUsingMap = numbers.map(x => x*x).reduce((total, value) => total + value) ; console.log(sumOfSquareUsingForEach); // 55 console.log(sumOfSquareUsingMap); // 55
当须要多个操做时,使用forEach()
方法是一项很是乏味的工做。咱们能够在这种状况下使用map()
方法。微信
// Array: var numbers = []; for ( var i = 0; i < 1000000; i++ ) { numbers.push(Math.floor((Math.random() * 1000) + 1)); } // 1. forEach() console.time("forEach"); const squareUsingForEach = []; numbers.forEach(x => squareUsingForEach.push(x*x)); console.timeEnd("forEach"); // 2. map() console.time("map"); const squareUsingMap = numbers.map(x => x*x); console.timeEnd("map");
这是在MacBook Pro的 Google Chrome v83.0.4103.106(64位)上运行上述代码后的结果。 建议复制上面的代码,而后在本身控制台中尝试一下。dom
forEach: 26.596923828125ms map: 21.97998046875ms
显然,map()
方法比forEach()
转换元素要好。
这两种方法都不能用 break
中断,不然会引起异常:
const numbers = [1, 2, 3, 4, 5]; // break; inside forEach() const squareUsingForEach = []; numbers.forEach(x => { if(x == 3) break; // <- SyntaxError squareUsingForEach.push(x*x); }); // break; inside map() const squareUsingMap = numbers.map(x => { if(x == 3) break; // <- SyntaxError return x*x; });
上面代码会抛出 SyntaxError
:
ⓧ Uncaught SyntaxError: Illegal break statement
若是须要中断遍历,则应使用简单的for循环或for-of
/for-in
循环。
const numbers = [1, 2, 3, 4, 5]; // break; inside for-of loop const squareUsingForEach = []; for(x of numbers){ if(x == 3) break; squareUsingForEach.push(x*x); }; console.log(squareUsingForEach); // [1, 4]
建议使用map()
转换数组的元素,由于它语法短,可连接且性能更好。
若是不想返回的数组或不转换数组的元素,则使用forEach()
方法。
最后,若是要基于某种条件中止或中断数组的便利,则应使用简单的for循环或for-of
/ for-in
循环。
代码部署后可能存在的BUG无法实时知道,过后为了解决这些BUG,花了大量的时间进行log 调试,这边顺便给你们推荐一个好用的BUG监控工具 Fundebug。
原文:https://codingnconcepts.com/j...
文章每周持续更新,能够微信搜索「 大迁世界 」第一时间阅读和催更(比博客早一到两篇哟),本文 GitHub https://github.com/qq449245884/xiaozhi 已经收录,整理了不少个人文档,欢迎Star和完善,你们面试能够参照考点复习,另外关注公众号,后台回复福利,便可看到福利,你懂的。