{thisObject : [ 0: {property1: true,property2: 4},
1: {property3: 'hello',
property4: 'goodbye'},
2: {property5: 'imagine there are 2000 of these objects and each object has 30-50 properties',
property6: 'How in the world does one remove a specific object, along with the id, for example the object corresponding to id number 1'}
]}
我终其一生都无法弄清楚如何从状态中删除一个 id 的不变性。任何帮助将不胜感激。
答案 0 :(得分:0)
我经常建议将数据存储在以 id 为键的键值对象中,因为它们比数组更容易使用。但在这种情况下 - 删除与给定 id 匹配的项目 - 实际上使用数组很容易,因为 array.filter()
返回一个新数组,因此可以安全使用。
假设 thisObject
是 state
的属性,id
是要删除的 id:
return {
...state,
thisObject: state.thisObject.filter( obj => obj.id !== id ),
}