Skip to content

Instantly share code, notes, and snippets.

@manatax
Last active May 29, 2017 22:19
Show Gist options
  • Save manatax/2c750a33a7f8540f7a1646503855be6e to your computer and use it in GitHub Desktop.
Save manatax/2c750a33a7f8540f7a1646503855be6e to your computer and use it in GitHub Desktop.
flatten an array of arbitrarily nested arrays
let data = [[1,2,[3]],4];
function flatten(array) {
let res = [];
for (let item of array) {
if (Array.isArray(item)) {
res = res.concat(flatten(item));
} else {
res.push(item);
}
}
return res;
}
console.log(flatten(data));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment