Skip to content

Instantly share code, notes, and snippets.

@sebringj
Last active July 21, 2017 15:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sebringj/c8e55d8e06f4a6d9e5342d7dc50b5f26 to your computer and use it in GitHub Desktop.
Save sebringj/c8e55d8e06f4a6d9e5342d7dc50b5f26 to your computer and use it in GitHub Desktop.
flattens nested list of arrays of integers
// 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