let A1 = [1, 2, 3, 4, 5];
//两种复制办法
// A1复制给B1
let B1 = A1;
console.log(B1);
// A1复制给C1
let C1 = A1.slice();
console.log(C1);
// D 复制A1的前三项
let D1 = A1.slice(0, 3);
console.log(D1);
//B2 C2复制A2
var A2 = [1, 2, 3, 4, 5, 6, 7], B2 = A2.slice(), C2 = A2.slice();
console.log(B2);
console.log(C2);
// 把10加入数组A2
A2.push(10);
console.log(A2);
// 获取被移除A2的元素
let removedElement = A2.pop();
console.log(removedElement);
C2.splice(3, 2, 10, 11, 12); // 从数组第三个元素后移除两个元素,从第三个元素后开始增加指定元素
console.log(C2);
// 在A2前面追加元素99
A2.splice(0, 0, 99);
console.log(A2);
输出结果:
[ 1, 2, 3, 4, 5 ]
[ 1, 2, 3, 4, 5 ]
[ 1, 2, 3 ]
[ 1, 2, 3, 4, 5, 6, 7 ]
[ 1, 2, 3, 4, 5, 6, 7 ]
[ 1, 2, 3, 4, 5, 6, 7, 10 ]
10
[ 1, 2, 3, 10, 11, 12, 6, 7 ]
[ 99, 1, 2, 3, 4, 5, 6, 7 ]