Skip to content

Instantly share code, notes, and snippets.

@JoxieMedina
Last active January 12, 2018 18:37
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoxieMedina/d56c4c273e4263995f2557e160c87d93 to your computer and use it in GitHub Desktop.
Save JoxieMedina/d56c4c273e4263995f2557e160c87d93 to your computer and use it in GitHub Desktop.
Arbitrarily nested arrays of integers into a flat array of integers
/*
Write some code, that will flatten an array of arbitrarily nested arrays
of integers into a flat array of integers. e.g. [[1,2,[3]],4] -> [1,2,3,4]
I use JS in ES6 Syntax
Tested in Chrome, Firefox and Safari
*/
let flatten = []
evaluate = (el=[0,[[1, 2],[3]],[4,[[5,6]]]])=>{
el.map(n=>n instanceof Array ? evaluate(n) : flatten = [...flatten,n])
}
evaluate()
console.log('Flatten: ',flatten)//Flatten: [ 1, 2, 3, 4, 5, 6 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment