Created
February 6, 2019 15:27
-
-
Save carlosbensant/c176905a7c8c38aea6c5bba6eaad0bfa to your computer and use it in GitHub Desktop.
Flatten - Javascript Function
This file contains hidden or 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
myArray = [[1,2,[3]],4]; // -> [1,2,3,4] | |
function flatten(arrayVal) { | |
return arrayVal.reduce((newArray, currentValue) => Array.isArray(currentValue) ? newArray.concat(flatten(currentValue)) : newArray.concat(currentValue), []) | |
} | |
console.log('flatten: ', flatten(myArray)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment