有幾種方法可以有效地合并兩個數組:
let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];
let mergedArray = arr1.concat(arr2);
console.log(mergedArray); // [1, 2, 3, 4, 5, 6]
let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];
let mergedArray = [...arr1, ...arr2];
console.log(mergedArray); // [1, 2, 3, 4, 5, 6]
let arr1 = [1, 2, 3];
let arr2 = [4, 5, 6];
arr2.forEach(element => {
arr1.push(element);
});
console.log(arr1); // [1, 2, 3, 4, 5, 6]
這些方法都可以有效地合并兩個數組,你可以根據自己的需求選擇適合的方法。