Created
March 2, 2017 22:41
-
-
Save tqg5/9732c8fd48093eb27ee692dc8b76ab7d to your computer and use it in GitHub Desktop.
Flatten Array
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,6]]],[4,10,23,[3,5,[56]]]]; | |
function flattenArr(val) { | |
if(val instanceof Array) { | |
return val.reduce(function(acc, val) { | |
return acc.concat(flattenArr(val)); | |
},[]); | |
} | |
return [val]; | |
} | |
var newArr = arr.reduce(function(acc, val) { | |
return acc.concat(flattenArr(val)); | |
}, []); | |
console.log(newArr); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment