In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-01 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article introduces the relevant knowledge of "which are the five methods of array array". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
1. Array deformation Transform (pure function of function normal form)
First list the functions that do not have side-effects for array morphing.
1) reduce 2) map 3) flat 4) flatMap 5) fill 6) forEach. ForEach is not pure-function, but it belongs to implicit iterative method, so it is classified here.
/ / 1.reduce let total = [0,1,2,3] .reduce ((acc, cur) = > acc + cur, 0); / / 2.map const maped = [4,7,9,18] .map (x = > x * 2); / / 3.flat let arr2 = [5,2,3] .flat (); / / 4.flatMap [5,8,9,6] .flatMap (x = > [x * 2]) / / 5.fill is a nonpure function, but because it is often used to build range for iteration, it is classified here as an Array (5) .fill (0) / / 6.forEach impure function as an alternative to map for dealing with side-effect problems. ['x','y','z'] .forEach (el = > console.log (el)); array logic judgment logic predicates (pure function of function normal form)
After the six methods of the function normal form, we continue to examine the higher-order functions used for logical judgment:
1) filter 2) find 3) findIndex 4) includes 5) indexOf 6) some 7) every and we can build our own helpful range and not.
Where include is where find is applied to an element, and indexOf is findIndex is used for an element.
/ / 1.filter [23,76,98,101] .filter (v = > v > 30 & v)
< 100); //[ 76, 98 ] // 2.find 只需要单个元素则用 find [23, 76, 98, 101].find( v =>V > 30 & & v
< 100); // 76 // 3.findIndex 查找单个元素的index [23, 76, 98, 101].findIndex( v =>V > 30 & & v
< 100); // 1 // 4.includes 是 find 查找特定元素 [23, 76, 98, 101].includes(77) // false // 5.indexOf 是 findIndex 查找某个特定元素的 index,返回值为 -1 [23, 76, 98, 101].indexOf(77) // -1 // 6.some [23, 76, 98, 101].some(v =>V > 30 & & v
< 100) //true // 7.every [23, 76, 98, 101].every(v =>V > 30 & & v
< 100) //false三、非函数式的数组变形(纯函数) 以上两组12个函数均为函数范式编程的纯函数。接下来考察,其他对数组变形的纯函数。(纯函数是指没有side-effect副作用的函数): 1) concat 2) join 3) slice 4) splice (非纯函数,将会修改原数组,放在此处只与slice对比,作为提醒) // 1.concat ['x', 'y', 'z'].concat([9, 8, 7]); // 2.join ['x', 'y', 'z'].join(","); // 3.slice ['x', 'y', 'z'].slice(1, 3); // 4.splice放到第四组中,此处只为提醒与slice相对比。四、操作数据结构 (非纯函数) Array可以作为两种抽象结构数据的载体:分别为 stack 和 queue。 1) push 2) pop 3) shift 4) unshift 5)splice(splice属于特殊方法,因为更改了原数组,放在此处) let arr = [23, 76, 98, 101]; // 1. push 元素添加到尾部 >Arr.push (1205) > console.log (arr) [23,76,98,101,120] / / above push and pop are combined to construct stack data structure. > arr.pop () 120 > arr [23,76,98,101] / / 3.shift takes elements from the array header and combines with push to construct queue data structure > arr.shift () 23 > arr [76,98,101] / / 4. Unshift adds elements from the array header > arr.unshift (59,145) 5 > arr [59,145,76,98,101] / 5.splice is a special method. For splicing arrays > arr.splice (1, 2, 55, 66, 77, 88) [145,76] > arr [59, 55, 66, 77, 88, 98101] V, array sorting (non-pure function)
It ends with a ubiquitous sort, and both sort and reverse are modified directly on the original array, that is, the inplace operation.
1) sort 2) reverse
/ / 1. Sort [23,76,98,101] .sort ((xPowery) = > x-y) / / 2.reverse [23,76,98,101] .reverse () VI. Summary of mind maps
This is the end of the content of "what are the five methods of array array". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
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.