In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "the method tutorial of building 12 functions such as Map from the array Reduce". In the daily operation, I believe that many people have doubts about the method tutorial of building 12 functions such as Map from the array Reduce. Xiaobian 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 "the method tutorial of building 12 functions such as Map from the array Reduce". Next, please follow the editor to study!
I. the application of array.reduce at the beginning of all things
The core of Reduce is dimensionality reduction, which converts the array reduce to a value, such as summation:
Const arr = [52,71,27,38]; const sum = (x, y) = > x + y; const cusSum = arr.reduce (sum, 0)
Use reduce as a thinking tool, and always keep the initial value of initial-value in your mind.
Second, construct array.map from mathematics to programming
Map is mathematical thinking and goes directly into programming, which is simulated from reduce as follows:
Const cusMap = (arr, fn) = > arr.reduce ((x, y) = > x.concat (fn (y)), [])
Third, construct the array.flat array.flatMap leveling array
From array.flat we get a glimpse of the advantages of declaratively programming by focusing on what you want to do without paying attention to the implementation details. Implemented in reduce as follows:
When only flat reaches one level of depth:
# flat only to one level const flat1 = arr = > [] .concat (.arr); const flat2 = arr = arr.reduce (acc, v = > acc.concat (v), [])
When you need to flat to any depth, completely ReFactor flat with reduce:
If (! Array.prototype.flat) {Array.prototype.flat = function (n = 1) {this.flatAllX = () = > this.reduce ((f, v) = > f.concat (Array.isArray (v)? V.flat (Infinity): v), []); this.flatOneX = () = > this.reduce ((f, v) = > f.concat (v), []); return n = Infinity? This.flatAllX (): n = = 1? This.flatOneX (): this.flatOneX (). Flat (n-1);};}. Array.filter moves towards higher-order logical judgment logic predicate
Why do you want to rebuild with reduce, because it helps to polish the concept of function and final output acculator all the time in your mind.
Const cusFilter = (arr, fn) = > arr.reduce ((acc, val) = > (fn (val)? Acc.concat (y): acc), []); 5. Array.find and array.findIndex only find the first element
Array.filter will filter out all the elements that meet the requirements, and apply array.find when we only need a single element.
Const cusFind = arr.reduce ((acc, val) = > (acc = undefined & & fn (val)? Val: acc), undefined)
Rebuild array.findIndex:
Const cusFindIndex = arr.reduce ((x, y, I) = > (x = =-1 & & fn (y)? I: X),-1)
Furthermore, we use find and findIndex to simply build includes and indexOf.
Arr.includes (value); / / arr.find (v = > v = value) arr.indexOf (value); / / arr.findIndex (v = > v = value) VI. Fast array.some and array.every
Although the array and some functions are simple, they are especially easy to think about and use.
/ / arr.every (fn); arr.reduce ((a, v) = > a & & fn (v), true); / / a for accumulator, / / arr.some (fn); arr.reduce ((a, v) = > a | | fn (v), false); / / v for value so far, the study on "how to build Map and other 12 functions from array Reduce" is over, hoping to 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.