Last active
July 9, 2017 15:37
-
-
Save imballinst/864ca78b53912fb60f5cbad3bd9467da to your computer and use it in GitHub Desktop.
Alternative to Array.prototype.reduce()
This file contains hidden or 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
const arr = [1, [2], [3, [[4], [5]]]]; | |
let x = []; | |
function recurse(arr) { | |
let dest = []; | |
if (Array.isArray(arr)) { | |
arr.forEach(el => { | |
dest = dest.concat(recurse(el)); | |
}); | |
} else { | |
dest.push(arr); | |
} | |
return dest; | |
} | |
x = recurse(arr); | |
console.log(x); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment