Skip to content

Instantly share code, notes, and snippets.

@smithaitufe
Created April 8, 2019 14:42
Show Gist options
  • Save smithaitufe/3087db58ce6419effb10305b62095cdb to your computer and use it in GitHub Desktop.
Save smithaitufe/3087db58ce6419effb10305b62095cdb to your computer and use it in GitHub Desktop.
A function to flatten array
function flatten(arr, result) {
for(let i = 0; i < arr.length; i++){
if(Array.isArray(arr[i])){
flatten(arr[i], result)
}else {
result.push(arr[i])
}
}
return result
}
console.log(flatten([[1,2,[3]],4], []))
console.log(flatten([[1,2,[3, [5, 6, [9, 8, 7]]],4]], []))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment