react redux reducer中缺少数组

时间:2019-03-01 08:01:33

标签: javascript node.js reactjs redux

我正在尝试检查我的内部数组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 }

1 个答案:

答案 0 :(得分:2)

{}内展开数组时,它将创建一个对象,其数组索引为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 ) ]