In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "what are the common methods of adding arrays in es6". In daily operation, I believe that many people have doubts about the common methods of adding arrays in es6. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "what are the common methods of adding arrays in es6?" Next, please follow the editor to study!
Es6 array added four commonly used methods are: 1, forEach (), used to traverse the array, no return value; 2, filter (), filter the array does not meet the conditions; 3, map (), traverse the array, return a new array; 4, reduce (), let the two items before and after the array for some calculation, and then return its value.
The operating environment of this tutorial: windows7 system, ECMAScript version 6, Dell G3 computer.
Four practical array methods have been added to ES6, which are: forEach,filter,map and reduce.
1.forEach
Traversing the array, no return value, no change to the original array, just traversing-- equivalent to for loop traversal
Let arr=] let res=arr.forEach (item= > {console.log (item); / / 234Power56) let res2=arr.forEach ((item,index) = > {console.log (item,index); / / 23 0 / / 441 / /.... })
2.filter:
The filter () function filters out the values in the array that do not meet the conditions. If the callback function returns true, it stays and returns a new array without changing the original array,!! It can be used to do array deduplication.
Let arr = [1, 5, 2, 16, 4, 6] / / 1. Find out the even-numbered let newArr=arr.filter ((vMagne) = > v% 2records 0) console.log (newArr,'newArr') in the array; / / 2meme 16meme 4jin6 / / 2. The filter is retained as the result of TRUE let arrTue= [13 let newArr2=arrTue.filter 14 true:false 9 Magi 33] let newArr2=arrTue.filter (item= > (item%3===0)? true:false) console.log (newArr2,'newArr2'); / / 9 Magne33 / / 3. Use filter to remove weight'/ / the first kind of let arr3= [2 item 3 5 item,1 1 2 item,1 3 4 5 5 6 8], newArr3 function unique (arr) {const res = new Map () return arr.filter ((item) = >! res.has (item) & & res.set (item,1))} console.log (newArr3=unique (arr3), 'newArr3') / 2 key 3jorie 5jorie 4jingle 6jingo / second kind let testArray = [{key: "01", name:' Zhang San'}, {key: "02", name:' Xiao Li'}, {key: "03", name:' Xiao Luo'}, {key: "04", name:' Zhang San'}, {key: "03", name:' Xiao Li'},] Let deduplicationAfter = testArray.filter ((value,index,array) = > {/ / findIndex the position of the first element of the array that matches the criteria. Return array.findIndex (item= > item.key = value.key & & item.name = value.name) = index}) console.log (deduplicationAfter)
3.map:
Map traverses the array, returns a new array, does not change the original array, maps one to one, maps to a new array
Let arr = [6 result 10, 12 return item*2 8] let result = arr.map (function (item) {return item*2}) let result2 = arr.map (item= > item*2) / / the abbreviation of the arrow function, if you want to understand, the following article introduces console.log (result) console.log (result2) let score = [18,86,88,24] let result3 = score.map (item= > item > = 60? 'pass': 'fail') console.log (result3)
4.reduce:
Reduce () sums up a heap (for example, to calculate a total, to calculate an average). Reduce allows the front and back items of the array to perform some calculation, then returns their value, and then continues the calculation without changing the original array, returning the final result of the calculation. If no initial value is given, traversal starts from the second term of the array.
Arr.reduce (callback (accumulator, currentValue [, index [, array]]) [, initialValue]), the first parameter callback function, the second parameter initial value
4.1 summation
/ / the first given initial value var arr = [1,3,5,7]; / / principle: use the reduce property prev initial value to set 0 and bring the return value of the function again into the function var sum = arr.reduce (function (tmp,item,index) {/ / prev is the value returned by the function console.log (tmp,item,index) after the initial value is 0) / / 0 10 / / 1 31 / / 4 52 / 9 7 3 return tmp + item; / / Array Sum}, 0); console.log (sum) / / 16 / / the second method does not give the initial value var arr2 = [1,3,5,7] var result = arr2.reduce (function (tmp, item, index) {/ / tmp last result, the current number of item Index times 1 start console.log (tmp, item, index) / / 1 31 / / 4 52 / / 9 7 3 return tmp + item }) console.log (result,) / / 16
4.2 find the average
Var arr = [1,3,5,7] var result = arr.reduce (function (tmp,item,index) {console.log (tmp,item,index)) / / 13 1 / / 4 52 / / 9 7 3 if (index! = arr.length-1) {/ / not the last return tmp + item} else {return (tmp + item) / arr.length}}) console.log (result,' [[u99') / / average 4, this ends the study of "what are the common methods for adding arrays in es6"? I hope I can solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.