Created
June 2, 2019 11:28
-
-
Save JoviDeCroock/29dd0f148330602d9a93d679612b5067 to your computer and use it in GitHub Desktop.
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 inputArray = [[1, 2, [3]], 4]; | |
const flatten = input => { | |
return input.reduce((acc, x) => { | |
if (Array.isArray(x)) { | |
return [...acc, ...flatten(x)]; | |
} else { | |
return [...acc, x]; | |
} | |
}, []); | |
}; | |
console.log(flatten(inputArray)); // [1, 2, 3, 4] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment