Created
February 23, 2022 18:33
-
-
Save gpDA/69206c1df3e12f728dd773c979d115b4 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
function flatten(arr, ret = []) { | |
for (const entry of arr) { | |
if (Array.isArray(entry)) { | |
flatten(entry, ret) | |
} else { | |
ret.push(entry) | |
} | |
} | |
return ret; | |
} | |
const arr1 = [0, 1, 2, [3, 4]]; | |
const arr2 = [1, 2, [3, 4, 5, [6, 7]]]; | |
console.log(flatten(arr1)); // [ 0, 1, 2, 3, 4 ] | |
console.log(flatten(arr2)); // [1, 2, 3, 4, 5, 6, 7] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment