[英]Array missing in react redux reducer
i am trying to check my inner array id same as dispatched id, table example我正在尝试检查我的内部数组 id 与调度 id 相同,表示例
{
_id :1,
name: sagar elias jacky
Amenities :[{ id: 100, title : hi },{ id: 101, title : hallo } ]
}
checking dispatched id exit or not using map,检查调度的 id 退出或不使用地图,
return { ...state,
items : {...state.items,
Amenities : { ...state.items.Amenities
.map(x=> x._id === action.dispatchedID ? {...x,deleting: true} : x ) }}}
but it will return with non array Amenities, like但它会返回非数组 Amenities,比如
Amenities:
0: { id: 100, title : hi },
1: { id: 101, title : hallo }
i want this to我想要这个
Amenities:Array(2)
0: { id: 100, title : hi },
1: { id: 101, title : hallo }
When you spread an array inside {}
, it creates an object with indexes of array as keys
当您在
{}
内展开一个数组时,它会创建一个以数组索引作为keys
的对象
const array = [{a:1}, {a:2}] console.log({...array})
So, change所以,改变
Amenities : { ...state.items.Amenities
.map(x=> x._id === action.dispatchedID ? {...x,deleting: true} : x ) }
to:到:
Amenities : [ ...state.items.Amenities
.map(x=> x._id === action.dispatchedID ? {...x,deleting: true} : x ) ]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.