Get the App
SLTechnology News&Howtos  ›  Development  › 

How to flatten the array by js

Shulou Source: shulou.com Published: 2022-06-02 06:55:45 11月13日 Update

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.

Tags: Array element flat method depth processing function problem one-dimensional recursive character string more operator help loop test solution operation flat Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Docker Shulou Technology MySQL MariaDB