Created
March 29, 2019 14:10
-
-
Save zoryana94/14b3e849de1c18f959c5c6e3c9ff3806 to your computer and use it in GitHub Desktop.
Flatten array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const arr = [[1,2,[3]],4]; | |
function flattenArray(arr = []) { | |
let flattenedArray = []; | |
arr.forEach(item => { | |
if (Array.isArray(item)) { | |
flattenedArray = [...flattenedArray, ...flattenArray(item)]; | |
} else { | |
flattenedArray.push(item); | |
} | |
}); | |
return flattenedArray; | |
} | |
flattenArray(arr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment