Created
June 11, 2018 12:21
-
-
Save svrsreeraj/a6603b1004777e4e31314d27d657061a to your computer and use it in GitHub Desktop.
flatten array
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
function flattenArray(inputArray, outputArray = []) { | |
for (let i = 0; i < inputArray.length; i++) { | |
const element = inputArray[i]; | |
if (typeof element === "object" && element.length > 0) { | |
flattenArray(element, outputArray) | |
continue; | |
} | |
outputArray.push(element); | |
} | |
return outputArray; | |
} | |
var test = [1,2,[5],[[6]],3,4]; | |
console.log(flattenArray(test)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment