Last active
August 29, 2015 14:13
-
-
Save bradoyler/622cc78de41e4e0a9c4f to your computer and use it in GitHub Desktop.
flatten a list / array in js
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
//input: [[1], 2, [[3,4], 5], [[[]]], [[[6]]], 7, 8, []] | |
function flatten(list) { | |
return list.reduce(function (acc, val) { | |
return acc.concat(val.constructor === Array ? flatten(val) : val); | |
}, []); | |
} | |
// output: [1, 2, 3, 4, 5, 6, 7, 8] | |
// more info: http://rosettacode.org/wiki/Flatten_a_list |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment