Use the splice function in react to delete an item in the array

1. The splice function
splice() method adds/deletes items to/from the array, and then returns the deleted item.
The slice() method can return selected elements from an existing array.
Therefore, when using it, you should pay attention to:

  • splice returns the deleted item

2. Give a small case I used in react: the
completed function is to delete an item of the array:
Insert picture description here

What I need to update here should be my intercepted array

  // 删除
  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,
    })
  }

The length of the original array is 3
printed values:
Insert picture description here
Insert picture description here

Guess you like

Origin blog.csdn.net/weixin_45416217/article/details/109440460