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/02 Report--
In order to solve the problem of how to flatten the array by js, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and easy way.
The way the array is flattened
What is array flattening?
Array flattening: refers to converting a multi-dimensional array into an one-dimensional array.
Example: flatten the following array.
Const arr = [1, [2,3, [4,5]] / /-- > [1,2,3,4,5] 1. Use flat ()
The flat () method, proposed by ES10, recursively traverses the array at a specified depth and merges all elements with elements in the traversed subarray into a new array. (flat means "horizontal; flat")
Const result1 = arr.flat (Infinity) / / specify a depth of infinite console.log (result1) / / [1,2,3,4,5] const result2 = arr.flat (1) / / specify a depth of 1console.log (result2) / / [1,2,3, [4,5]] const result3 = arr.flat (2) / / specify a depth of 2console.log (result3) / / [1,2,3,4,5] 2. Use regularities
The array elements obtained by doing the following will all become strings, and it is not recommended to use
Const result1 = JSON.stringify (arr). Replace (/\ [|\] / g,''). Split (',') console.log (result1) / / ['1','2','3','4','5'] array elements become strings
Optimize the above methods
Const result2 = JSON.parse ('['+ JSON.stringify (arr). Replace (/\ [|\] / g,') +']) console.log (result2) / / [1, 2, 3, 4, 5] 3. Use reduce () + concat ()
Use reduce to get the current value and the previous value of the array, determine whether the current value is an array, set the initial value to [], and then use concat to merge the array.
The reduce () method executes a reducer function (in ascending order) provided by you on each element in the array, summarizing the results into a single return value.
The concat () method is used to merge two or more arrays. This method does not change the existing array, but returns a new array.
Function flatten (arr) {return arr.reduce ((pre, current) = > {return pre.concat (Array.isArray (current)? Flatten (current): current)}, [])} const result = flatten (arr) console.log (result) / / [1,2,3,4,5] 4. Use function recursion
Loop through the array and find that it contains array elements for recursive processing, and finally convert the array into an one-dimensional array.
Const result = [] function exec (arr) {arr.forEach (item = > {if (Array.isArray (item)) {exec (item)} else {result.push (item)})} exec (arr) console.log (result) / / [1,2,3,4,5] 5. Use the extension operator + concat ()
ES6's new extension operator can reduce the dimension of the array (one dimension at a time), loop to determine whether it contains an array, and perform concat merging.
The some () method: tests whether at least one element in the array passes the supplied function test (which returns a value of type Boolean).
Function flatten (arr) {while (arr.some (item = > Array.isArray (item) {arr = [] .concat (... arr)} return arr} const result = flatten (arr) console.log (result) / / [1, 2, 3, 4, 5] this is the answer to the question about how js flattens the array. I hope the above can be helpful to you, if you still have a lot of questions to solve. You can follow the industry information channel for more related knowledge.
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.