【发布时间】:2026-01-11 17:00:01
【问题描述】:
我的 Redux 状态存储了一个 comments 对象,该对象存储了一个 cmets 数组。每个comment 对象中都有一个replies 数组。我将父评论 ID 和回复 ID 都传递给我的 reducer,目的是从回复数组中删除回复。
我的* cmets 对象的简化版本如下所示:
{
"count": 4,
"data": [
{
id: 123,
text: 'This is a comment',
replies: [
{
id: 479,
text: 'This is a reply',
},
{
id: 293,
text: 'This is another reply',
},
],
},
{
id: 728,
text: 'This is another comment',
replies: [
{
id: 986,
text: 'This is a reply',
},
],
},
],
"pageSize": 5,
cursor: "",
}
这是我的减速器,它似乎将父评论对象包装在一个数组中并展平回复(显然不是预期的结果,但我不知道解决嵌套数组的最佳方法)。
case types.DELETE_REPLY_SUCCESS: {
const content = Object.assign({}, state);
content.data = content.data.map((comment) => {
const newObj = { ...comment };
if (newObj.id === action.parentCommentId) {
return newObj.replies.filter(reply => reply.id !== action.replyId);
}
return newObj;
});
return content;
}
【问题讨论】:
标签: javascript arrays ecmascript-6 redux