javascript - React Redux reducer 中缺少数组

标签 javascript node.js reactjs redux

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

相关文章:

javascript - Node.js错误: require is not defined in clientside js file

javascript - 为什么 "new Date()"返回不同格式的时间,具体取决于我运行的 Node 服务器?

node.js - 使用 Mongoose 搜索数组内的 _id

javascript - 在 React 中将变量传递给 onClick

reactjs - 如何将操作列设置为 Material ui 表 ReactJs 中的最后一列?

java - 在servlet上上传 Canvas base64图像

javascript - HTML 滑动条?

javascript - 在 Javascript 中,<int-value> == "<int-value>"的计算结果为 true。为什么会这样?

node.js - mongodb聚合: average and sorting

javascript - React-Redux 程序返回 "TypeError: Cannot read property ' map' of undefined"