我试图检查我的内部数组id与调度id相同,表示例

{
 _id :1,
 name: sagar elias jacky
 Amenities :[{ id: 100, title : hi },{ id: 101, title : hallo } ]
}

正在检查调度的ID出口是否使用映射,
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 }

最佳答案

当您在{}内展开数组时,它会创建一个数组索引为keys的对象。

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/

10-11 07:18