如何解决如何从嵌套对象 redux 状态中删除项目
{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 的不变性。任何帮助将不胜感激。
解决方法
我经常建议将数据存储在以 id 为键的键值对象中,因为它们比数组更容易使用。但在这种情况下 - 删除与给定 id 匹配的项目 - 实际上使用数组很容易,因为 array.filter()
返回一个新数组,因此可以安全使用。
假设 thisObject
是 state
的属性,id
是要删除的 id:
return {
...state,thisObject: state.thisObject.filter( obj => obj.id !== id ),}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。