Skip to content

Instantly share code, notes, and snippets.

@elitenomad
Last active February 21, 2020 01:26
Show Gist options
  • Save elitenomad/99e53452caf8cb0d1f13fa41a4715aa5 to your computer and use it in GitHub Desktop.
Save elitenomad/99e53452caf8cb0d1f13fa41a4715aa5 to your computer and use it in GitHub Desktop.
Flatten array of nested arrays Javascript
function flatten(arr) {
a = []
arr.forEach((s) => {
if(Array.isArray(s)){
a = a.concat(flatten(s))
}else{
a.push(s)
}
})
return a
}
//Use case 1
[1,2,3,4]
//Use case 2
[1,[2,3],4]
//use case 3
[1,[2,3],[4,5,[6,7],8],9,10]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment