Created
February 13, 2018 21:15
-
-
Save brunorafael8/919e03d4eaf6a9a8294cd7648f704acb to your computer and use it in GitHub Desktop.
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
/** | |
* Returns a nested array into a flatten single level array | |
* @param {Array} input a multiple level array | |
* @return {Array} an one level array of all the elements inside | |
*/ | |
const result = []; | |
const flatten = array => { | |
for (var i = 0; i < array.length; i++) { | |
if (Array.isArray(array[i])) { | |
flatten(array[i]); | |
} else { | |
result.push(array[i]); | |
} | |
} | |
return result; | |
} | |
const test = [[1,2,[3]],4]; | |
console.log(flatten(test)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment