我正在尝试检查我的内部数组ID是否与调度ID相同,表示例
{
_id :1,
name: sagar elias jacky
Amenities :[{ id: 100, title : hi },{ id: 101, title : hallo } ]
}
检查调度 ID 退出或不使用 map ,
return { ...state,
items : {...state.items,
Amenities : { ...state.items.Amenities
.map(x=> x._id === action.dispatchedID ? {...x,deleting: true} : x ) }}}
但它将返回非数组设施,例如
Amenities:
0: { id: 100, title : hi },
1: { id: 101, title : hallo }
我想要这个
Amenities:Array(2)
0: { id: 100, title : hi },
1: { id: 101, title : hallo }
最佳答案
当您在 {}
内展开数组时,它会创建一个以数组索引作为键
的对象
const array = [{a:1}, {a:2}]
console.log({...array})
所以,改变
Amenities : { ...state.items.Amenities
.map(x=> x._id === action.dispatchedID ? {...x,deleting: true} : x ) }
至:
Amenities : [ ...state.items.Amenities
.map(x=> x._id === action.dispatchedID ? {...x,deleting: true} : x ) ]
关于javascript - React Redux reducer 中缺少数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54940319/