Javascript 反应本机从数组中删除对象

Javascript 反应本机从数组中删除对象,javascript,react-native,Javascript,React Native,我试图从数组中删除一个对象,尽管我不确定如何获取该对象,这基本上使得编写的代码没有任何作用 让我展示一下现有的阵列: Object { "created_at": "2020-06-30T11:22:53.000000Z", "icon": "subjectImgs/509-1080x1080.jpg", "id": 2, "name": &qu

我试图从数组中删除一个对象,尽管我不确定如何获取该对象,这基本上使得编写的代码没有任何作用

让我展示一下现有的阵列:

Object {
    "created_at": "2020-06-30T11:22:53.000000Z",
    "icon": "subjectImgs/509-1080x1080.jpg",
    "id": 2,
    "name": "Test",
    "updated_at": "2020-06-30T11:22:53.000000Z",
    "user_id": 1,
  },
  Object {
    "created_at": "2020-06-04T18:32:25.000000Z",
    "icon": "subjectImgs/698-1080x1080.jpg",
    "id": 1,
    "name": "history",
    "updated_at": "2020-06-04T18:32:25.000000Z",
    "user_id": 1,
  }, 
  Object {
    "created_at": "2020-07-08T16:32:03.000000Z",
    "icon": "subjectImgs/698-1080x1080.jpg",
    "id": 21, // Here is the ID
    "name": "123",
    "updated_at": "2020-07-08T16:32:03.000000Z",
    "user_id": 1,
  },
}
在侦听事件时,我会获取刚刚删除的对象的数组:

Object {
  "socket": null,
  "subject": Object {
    "created_at": "2020-07-08T16:32:03.000000Z",
    "icon": "subjectImgs/698-1080x1080.jpg",
    "id": 21, // Here is the ID
    "name": "123",
    "updated_at": "2020-07-08T16:32:03.000000Z",
    "user_id": 1,
  },
}
现在,我尝试删除它的方式如下:

            .listen('SubjectRemoved', ev => {
                var array = this.state.subjects
                this.setState({ subjects: array.filter((id) => id != ev.subject.id) })
            });

但显然,这是行不通的。我假设由于使用了
id
实际上什么也得不到。

我认为问题在于过滤方法:

array.filter((id) => id != ev.subject.id) // here id is an elemt of the array not the real id
你可以试试

array.filter((subject) => subject.id != ev.subject.id) //if is an array of subjects 

如果您认为
ev.subject.id
不起作用,您可以在筛选检测之前将其记录下来

array.filter((elem) => elem.subject.id != ev.subject.id) //if is an array of objects like {socket: null, subject: {... with the fields }}