Last active
January 12, 2018 18:37
-
-
Save JoxieMedina/d56c4c273e4263995f2557e160c87d93 to your computer and use it in GitHub Desktop.
Arbitrarily nested arrays of integers into a flat array of integers
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
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