The elements in an array are all arrays. How do you merge all the elements in an array into one array?

like this
[array [xxx], array [xxx], array [xxx].]

merge all xxx in array into one array [xxx.]


var arr = [[1,2,3],["a","b","c"],["",""]]
console.log(Array.prototype.concat.apply([],arr))//[1, 2, 3, "a", "b", "c", "", ""]

var flattened = [[0, 1], [2, 3], [4, 5]].reduce(
  function(a, b) {
    return a.concat(b);
  },
  []
);
// flattened is [0, 1, 2, 3, 4, 5]

javascript reference documentation


var arr = [[1jue 2jue 3], ["a", "b", "c"], ["to", "and"]
var arc= []
arr.forEach ((item) = > {

 arc.push(...item)

})
console.log (arc)


if it is only 2 layers of data, you can use Li Shisheng's method, if not, you need to extend it for traversal.


const arrays = [[1, 2], [3, 4], [5, 6]]
let newArray = arrays.reduce((a, b) => a.concat(b));
// newArray is [1, 2, 3, 4, 5, 6]
Menu