const months = ['Jan', 'March', 'April', 'June']; months.splice(1, 0, 'Feb'); // 在索引为1的地方开始插入Feb console.log(months); // 输出结果后的months数组: ["Jan", "Feb", "March", "April", "June"] months.splice(4, 1, 'May'); /* 在索引号为4的地方替换1个元素, 注意此时的数组已经经过上面的插入操作变成了["Jan", "Feb", "March", "April", "June"] */ console.log(months); // 输出结果后的months数组: ["Jan", "Feb", "March", "April", "May"]
const array1 = ['a', 'b', 'c']; const array2 = ['d', 'e', 'f']; const array3 = array1.concat(array2); console.log(array3); /* 输出结果: Array ["a", "b", "c", "d", "e", "f"] 输出的是两个数组连接之后的结果,并没有改变array1和array2的原有数组 */
const plants = ['broccoli', 'cauliflower', 'cabbage', 'kale', 'tomato']; console.log(plants.pop()); // 输出结果为: "tomato" console.log(plants); // 此时的数组: ["broccoli", "cauliflower", "cabbage", "kale"]