Skip to content

Instantly share code, notes, and snippets.

@qborreda
Created October 16, 2017 14:35
Show Gist options
  • Save qborreda/8f4883e90d48f52bba77feccd21d2ea0 to your computer and use it in GitHub Desktop.
Save qborreda/8f4883e90d48f52bba77feccd21d2ea0 to your computer and use it in GitHub Desktop.
Flatten an array of numbers
function flattenArray(arr) {
let found = [];
arr.map((elem, index) => {
if (Array.isArray(elem)) {
found = found.concat(flattenArray(elem));
} else {
found.push(elem);
}
});
return found;
}
let testArray = [[1, 2, [3]], 4, [5, 6]];
let output = flattenArray(testArray);
console.log(output);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment