react中使用splice函数去删除数组的某一项

1、splice函数
splice() 方法向/从数组中添加/删除项目,然后返回被删除的项目。
slice() 方法可从已有的数组中返回选定的元素。
所以,在使用的时候,就要注意的是:

  • splice返回的是被删除的项目

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
打印值:
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_45416217/article/details/109440460
今日推荐