【发布时间】:2020-04-07 08:46:33
【问题描述】:
我正在尝试更新状态内的对象数组内的特定值(在本例中为列表内的注释)。在这种情况下,msgs 键内的注释。
const list = [{
id: 1,
text: "list text",
msgs: [{
id: 1,
comment: "comment",
commentID: "ab37afa-c17-e4f-f103-4715b72f14ec"
}]
},
{
id: 2,
text: "list text 2",
msgs: [{
id: 2,
comment: "comment2", <--- trying to update this value
commentID: "ab37afa-c17-e4f-f103-4715b72f14ec-2"
},
{
id: 2,
comment: "comment3",
commentID: "ab37afa-c11323127-e4f-f103-4715b72f14ec-2"
}]
}
];
我目前正在尝试使用减速器:
case "EDIT_COMMENT":
temp = state.filter((list => list.id === parseInt(action.comment.id))) //getting the list that I want
const msgs = [].concat(...temp).map(x=> ({
id: x.id,
comment : x.comment,
commentID : x.commentID
})) // trying to get the messages inside the state
// I honestly don't know what to put here
return msgs.map( msg => {
if(msg.commentID === action.payload.commentID){
return {...}
}
}
)
我无法获得使用减速器更新我想要的数据的答案/解决方案,如果有人可以帮助我解决问题,我将不胜感激。 提前谢谢,周末愉快:)
【问题讨论】:
-
为什么你的 id 不是唯一的?
-
@larz 我在 msgs 中创建了密钥 ID,因为我不知道如何访问它,我不知道我现在正在尝试做的事情是否有必要。每条评论都有自己独特的评论ID。设置这样的结构是不好的???。感谢您的评论
标签: javascript arrays reactjs object redux