数组的concat
方法,用于合并两个或多个数组。
它可以接受任意数量的参数,这些参数可以是数组或者其他类型的值(非数组类型的值会被转换为单个元素的数组),然后将它们按照顺序合并到调用 concat
方法的数组之后。
let array1 = [1, 2, 3];
let array2 = [4, 5, 6];
let array3 = [7, 8, 9];
// 将array2和array3合并到array1之后
let newArray = array1.concat(array2, array3);
console.log(newArray); // 输出: [1, 2, 3, 4, 5, 6, 7, 8, 9]
let numbers = [1, 2, 3];
let strings = ['a', 'b', 'c'];
let boolean = true;
let nullValue = null;
// 将strings, boolean, 和 nullValue 合并到numbers之后
let mixedArray = numbers.concat(strings, boolean, nullValue);
console.log(mixedArray); // 输出: [1, 2, 3, 'a', 'b', 'c', true, null]
还有其他方法,欢迎评论区评论!!!