Last active
July 21, 2017 15:46
-
-
Save sebringj/c8e55d8e06f4a6d9e5342d7dc50b5f26 to your computer and use it in GitHub Desktop.
flattens nested list of arrays of integers
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
// VIEW ON JSFIDDLE -> https://jsfiddle.net/v5scgqao/3/ | |
/* | |
flattens array without mutation | |
*/ | |
function flattenArray(arr) { | |
let arr2 = [] | |
function flatten(a) { | |
for (let i = 0; i < a.length; i++) { | |
if (Array.isArray(a[i])) | |
flatten(a[i]) | |
else | |
arr2.push(a[i]) | |
} | |
} | |
flatten(arr) | |
return arr2 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment