Created
July 26, 2017 15:06
-
-
Save Dobby89/a033dc4ee0376907cd16a121885c13ff to your computer and use it in GitHub Desktop.
Flatten a multi-dimensional array in JavaScript
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
var arr = [1,2,3,4,[5,5,[1,1,1],6,7],[1,2,[2,3,[100,[23,45,11,[600,599]],101,99],4],3]]; | |
function flattenArray(array) { | |
return array.reduce(function(accumulator, current) { | |
if (Array.isArray(current)) { | |
return accumulator.concat(flattenReduce(current)); | |
} else { | |
return accumulator.concat(current); | |
} | |
}, []); | |
} | |
flattenArray(arr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment