简体   繁体   English

如何在 React 中使用 JavaScript 从数组中删除 object 元素

[英]How to delete object element from an array using JavaScript in React

I want to Delete an element of items(Array) whose id matches with index in deleteItem(index).我想删除其 id 与 deleteItem(index) 中的索引匹配的 items(Array) 元素。
Below is the code, I've tried so far:以下是我迄今为止尝试过的代码:

  constructor(){
    super()        
    this.state = {
      items : [
        { id: 34523, type: 'a', data: 'aaa' },
        { id: 23452, type: 'b', data: 'bbb' }.
        { id: 45644, type: 'c', data: 'ccc' }
      ]
    }
  }

  deleteItem(index){
    const copyStateAr = Object.assign([], this.state.items)
    copyStateAr.splice(index, 1);
    this.setState({
      items: copyStateAr
    })  
  }  

Note: Above code is working fine when I taking id as 1, 2, 3. But In case if I delete 2nd element first then the 3rd element doesn't getting deleted.注意:当我将 id 设为 1、2、3 时,上面的代码工作正常。但如果我先删除第二个元素,那么第三个元素不会被删除。
Also, if possible suggest me other method or approach to do the same task ( it is kind of search (id) and delete element).此外,如果可能的话,建议我使用其他方法或方法来完成相同的任务(它是一种搜索(id)和删除元素)。

Your best bet here is:你最好的选择是:

  1. Don't delete by index , delete by id不要按索引删除,按id删除
  2. Use the callback form of setState so you're dealing with up-to-date information使用setState的回调形式,以便处理最新信息

That's because state updates may be asynchronous and may be batched, so relying on the index can set you up for weird edge case failures.那是因为 state 更新可能是异步的并且可能是批处理的,因此依靠索引可以为奇怪的边缘情况失败做好准备。

So:所以:

deleteItem(id) {
    this.setState(({items}) => ({items: items.filter(e => e.id !== id)}));
}

Just delete objects by ID只需按 ID 删除对象

deleteItem(id){
    this.setState({
      items: this.state.items.filter(x => x.id !== id)
    })  
  }  

Index keeps changing.指数不断变化。 so if you have所以如果你有

{ id: 34523, type: 'a', data: 'aaa' },
        { id: 23452, type: 'b', data: 'bbb' }.
        { id: 45644, type: 'c', data: 'ccc' }

deleted 2 index删除 2 索引

then structure becomes然后结构变成

{ id: 34523, type: 'a', data: 'aaa' },
      
        { id: 45644, type: 'c', data: 'ccc' }

so you better start deleting by id element.所以你最好开始按 id 元素删除。 ADD a condition if index is <= number of element and send message index not found.如果索引 <= 元素数并且未找到发送消息索引,则添加一个条件。 You can make a copy of original and delete from copy.您可以制作原始副本并从副本中删除。 So original will always have full data.所以原始数据总是有完整的。

You can delete the object with the specified id using.forEach higher order function, then change the state using setState;您可以使用.forEach 高阶function 删除具有指定id 的object,然后使用setState 更改state;

const newArray = [];
this.state.items.forEach(object => {
    if ( object.id != specifiedId ) newArray.push(object);
});

this.setState({items: newArray});

Hope it helps !希望能帮助到你 !

Just modify copystatear只需修改copystatear

const copyStateAr = Object.assign([], items).filter((value, i) => value.id !== id);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM