Skip to content

Instantly share code, notes, and snippets.

@thedanheller
Created January 21, 2017 03:56
Show Gist options
  • Save thedanheller/3d11fa5fdaf8bd772004a943b225dab9 to your computer and use it in GitHub Desktop.
Save thedanheller/3d11fa5fdaf8bd772004a943b225dab9 to your computer and use it in GitHub Desktop.
Flatten Array
const flattenArray = (arr) => {
const flattenLevel = arr => [].concat(...arr);
const hasArray = item => {return Array.isArray(item);}
do {
arr = flattenLevel(arr);
} while (arr.findIndex(hasArray) > 0);
return arr;
}
let array = [[1,2,[3]],4];
console.log(flattenArray(array));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment