1. 基本语法
新数组 = 老数组.concat(值1[, 值2[, ...[, 值N]]])
- 作用:用于合并两个或多个数组。此方法不会更改现有数组,而是返回一个新数组。
- 参数:值可以使用基本数据类型的,也可以是数组,还可以是两者组合起来的
2. 基本实例
2.1 合并两个数组
let a = [1,2,3,4,5,6];
let b = ['a','b','c','d','e'];
let c = a.concat(b);
console.log(a);
console.log(b);
console.log(c);
结果:
[1, 2, 3, 4, 5, 6]
["a", "b", "c", "d", "e"]
[1, 2, 3, 4, 5, 6, "a", "b", "c", "d", "e"]
2.2 合并三个数组
let a = [1,2,3,4,5,6];
let b = ['a','b','c','d','e'];
let c = ['1',2,'e','3','t'];
let d = a.concat(b,c);
console.log(a);
console.log(b);
console.log(c);
console.log(d);
结果:
[1, 2, 3, 4, 5, 6]
["a", "b", "c", "d", "e"]
["1", 2, "e", "3", "t"]
[1, 2, 3, 4, 5, 6, "a", "b", "c", "d", "e", "1", 2, "e", "3", "t"]
2.3 数值和数组
let a = [1,2,3,4,5,6];
let b = 100;
let c = ['1',2];
let d = a.concat(b,c);
console.log(a);
console.log(b);
console.log(c);
console.log(d);
结果:
[1, 2, 3, 4, 5, 6]
100
["1",2]
[1, 2, 3, 4, 5, 6, 100, "1", 2]
2.4 嵌套数组
let a = [1,2,3,4,5,6];
let b = 100;
let c = ['1',['a','b','c',1,4,5]];
let d = [[[1,2,3,4],[2,3,4,5,6],2,4,5],7,8,9]
let e = a.concat(b,c,d);
console.log(a);
console.log(b);
console.log(c);
console.log(d);
console.log(e);
实例结果
网友评论