Last active
May 6, 2019 07:15
-
-
Save jovey-zheng/4f58f65e37c2e28910e653485eb2e87c to your computer and use it in GitHub Desktop.
Flatten deep nested arrays.
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
// array flatten function | |
function flatten (arr) { | |
let result = [].slice.call(arguments)[1] || [] | |
for (let i = 0; i < arr.length; i++) { | |
if (Array.isArray(arr[i])) { | |
flatten(arr[i], result) | |
} else { | |
result.push(arr[i]) | |
} | |
} | |
return result | |
} | |
// invoke: flatten([1, 2, [3, 4, [{num: 5}, {num: 6}]]]) | |
// output: [1, 2, 3, 4, {num: 5}, {num: 6}] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment