Created
March 28, 2020 14:58
-
-
Save melwyn95/d528479382bddbe2cc0981db9b139e43 to your computer and use it in GitHub Desktop.
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
function flatten(xs) { | |
var itr = xs; | |
while (true) { | |
var ys = []; | |
var allFlat = true; | |
itr.forEach(x => !(allFlat = (!Array.isArray(x) && allFlat)) ? (ys = ys.concat(x)) : ys.push(x)); | |
if (allFlat) return ys; | |
itr = ys; | |
} | |
} | |
console.log(flatten([1, 2, [3, 4], [5, [6, [7]]]])); | |
console.log(flatten([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