1、splice函数
splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目。
slice() 方法可从已有的数组中返回选定的元素。
所以,在使用的时候,就要注意的是:
2、举一个我在react中使用的小案例:
完成的功能是删除数组的某一项:
这里我需要更新的应该是我截取后的数组
// 删除
deleteSelect = (index) => {
const { initSelectInputList } = this.state;
const a = initSelectInputList.splice(index, 1); // 输出返回值
console.log("返回值", a)
initSelectInputList.splice(index, 1);
console.log("截取后", initSelectInputList) // 输出原数组
this.initUpdateList(initSelectInputList);
this.setState({
initSelectInputList,
})
}
原数组的长度是3
打印值: