js splice()方法
时间: 2024-04-16 21:23:15 浏览: 119
JavaScript中的splice方法用法详解
以下是关于JavaScript中splice()方法的介绍和演示:
1. 删除数组中的元素并返回被删除的元素[^1]:
```javascript
let colors = ["red", "green", "blue"];
var removed = colors.splice(1, 2, "pink");
console.log(colors); // 输出:["red", "pink"]
console.log(removed); // 输出:["green", "blue"]
```
2. 删除数组中的元素[^2]:
```javascript
let array = [0, 1, 2, 3, 4, 5, 6];
console.log("原数组为:" + array);
let arrBack = array.splice(2, 1);
console.log("array:" + array);
console.log("arrBack:" + arrBack);
```
阅读全文