Skip to content

Instantly share code, notes, and snippets.

@tqg5
Created March 2, 2017 22:41
Show Gist options
  • Save tqg5/9732c8fd48093eb27ee692dc8b76ab7d to your computer and use it in GitHub Desktop.
Save tqg5/9732c8fd48093eb27ee692dc8b76ab7d to your computer and use it in GitHub Desktop.
Flatten Array
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