Skip to content

Instantly share code, notes, and snippets.

@emmabostian
Created March 28, 2020 13:10
Show Gist options
  • Save emmabostian/0fbe2ea041b4eb9389e93de7d18e308c to your computer and use it in GitHub Desktop.
Save emmabostian/0fbe2ea041b4eb9389e93de7d18e308c to your computer and use it in GitHub Desktop.
function flattenArr(arr) {
let flattenedArr = [];
function flatten(subArr) {
for (let i of subArr) {
if (Array.isArray(i)) {
flatten(i);
continue;
}
flattenedArr.push(i);
}
}
flatten(arr);
return flattenedArr;
}
console.log(flattenArr([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