How to flatten an arrayWhat is array flattening? Array flattening : refers to converting a multidimensional array into a one-dimensional array. Example: Flatten the following array. const arr = [1, [2, 3, [4, 5]]] // ---> [ 1, 2, 3, 4, 5 ] 1. Using flat()The flat() method was proposed by ES10. It recursively traverses the array at a specified depth and merges all elements with the elements in the traversed sub-array into a new array and returns it. (flat means "horizontal; flat") const result1 = arr.flat(Infinity) // specifies the depth to be infinite console.log(result1) // [ 1, 2, 3, 4, 5 ] const result2 = arr.flat(1) // Specify depth as 1 console.log(result2) // [ 1, 2, 3, [ 4, 5 ] ] const result3 = arr.flat(2) // specifies the depth to be 2 console.log(result3) // [ 1, 2, 3, 4, 5 ] 2. Use regular expressionsThe array elements obtained by the following methods will be converted into strings, which is not recommended; const result1 = JSON.stringify(arr).replace(/\[|\]/g, '').split(',') console.log(result1) // [ '1', '2', '3', '4', '5' ] Array elements are converted to 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()
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
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 spread operator + concat()
some() method: Tests whether there is at least one element in the array that passes the provided function test (it returns a Boolean value). 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 ] SummarizeThis article ends here. I hope it can be helpful to you. I also hope you can pay more attention to more content on 123WORDPRESS.COM! You may also be interested in:
|
<<: Detailed explanation of how to quickly build a blog website using Docker
>>: Detailed explanation of the setting of background-image attribute in HTML
<br />The official version of Baidu Encyclop...
The network configuration of Host Only+NAT mode u...
Table of contents Boot Options Command Line Long ...
①. How to use the alias (CNAME) record: In the do...
The first step is to check the version number and...
This article shares the specific code of Vue to i...
Preface Crond is a scheduled execution tool under...
1. Favicon.cc To create ico icon websites online,...
Recorded the installation and use tutorial of MyS...
What is SQL? SQL is a language used to operate da...
In HTML, the Chinese phrase “學好好學” can be express...
This article shares the specific code of jQuery t...
Table of contents v-model .sync The difference in...
The layui table has multiple rows of data. Throug...
The mobile version of the website should at least...