here I flattened a two dimensional array like the following
function flatArray(a, b) {
return a.concat(b);
}
console.log([
['1', '2', '3'],
['4', '5', '6']
].reduce(flatArray));
See Question&Answers more detail:oshere I flattened a two dimensional array like the following
function flatArray(a, b) {
return a.concat(b);
}
console.log([
['1', '2', '3'],
['4', '5', '6']
].reduce(flatArray));
See Question&Answers more detail:osA different approach, works in situ.
function flatten(array) {
var l = array.length, temp;
while (l--) {
if (Array.isArray(array[l])) {
flatten(array[l]);
temp = array[l].slice();
temp.unshift(1);
temp.unshift(l);
[].splice.apply(array, temp);
}
}
}
var array = [['1', '2', '3'], ['4', '5', ['6'], ['7', '8']]];
flatten(array);
console.log(array);