In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "what are the differences between map and foreach in es6". Interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "what are the differences between map and foreach in es6"!
Difference: 1, forEach() method will not return the execution result, the return value is "undefined", while map() method will return the operation result, will return an array;2, forEach() method will modify the original array, while map() method will not modify the original array.
Operating environment of this tutorial: Windows 7 system, ECMAScript version 6, Dell G3 computer.
foreEach() method: Executes the provided function for each element.
map() method: Creates a new array where each element results from calling each element of the array to execute the provided function.
difference
The forEach() method does not return execution results, but undefined. That is, forEach() modifies the original array. The map() method gets a new array and returns it.
Examples:
Make an array squared
There is an array
let arr =[1,2,3,4,5,6]
ForEach() and Map()
forEach()
Note that forEach does not return a meaningful value.
We modify arr directly in the callback function.
arr.forEach((value, key) => { return arr[key] = value * value;});
The results are as follows:
Map()
let list = arr.map(value => { return value * value;});
The results are as follows:
Execution speed comparison
forEach() execution speed
< map()的执行速度 如何使用 forEach适合于你并不打算改变数据的时候,而只是想用数据做一些事情 - 比如存入数据库或则打印出来。 let arr = ['a', 'b', 'c', 'd'];arr.forEach((letter) =>{ console.log(letter);});// a// b// c// d
map() applies when you want to change data values. Not only is it faster, but it returns a new array. The advantage of this is that you can use composition (map(), filter(), reduce(), etc.) to play more tricks.
let arr = [1, 2, 3, 4, 5];let arr2 = arr.map(value => value * value).filter(value => value > 10);// arr2 = [16, 25]
We first multiply each element by itself using map, and then filter out those elements greater than 10. The final result is assigned to arr2.
At this point, I believe that everyone has a deeper understanding of "what is the difference between map and foreach in es6", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.