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
Copy code The code is as follows: <html> &l...
The user organization has two Windows Server 2008...
Introduction: This article takes the sample proje...
Linux change hostname command 1. If you only need...
Docker installation (Alibaba Cloud Server) Docker...
Related article: Beginners learn some HTML tags (1...
<br />Original link: http://www.dudo.org/art...
"Page screenshot" is a requirement ofte...
We will use CSS3 animated transitions to create a...
Table of contents Filters 01.What is 02. How to d...
Table of contents 1. jsonp cross-domain 2. docume...
Traceroute allows us to know the path that inform...
Everything is a file! UNIX has already said it. E...
This article shares the installation and configur...
Using Javascript to implement countdown to close ...