我有你可以回复的评论。我需要检查评论是否包含答案。
我得到了什么:
let сomments = [{сomment_id: 1, product_id: 1, ...}, {сomment_id: 2, product_id: 1, ...}, .... ];
let nested_comments = [{nested_id: 1, сomment_id: 1,product_id: 1},{nested_id: 2, сomment_id: 1, product_id: 1},...];
for (let i = 0; i < nested_comments.length; i++) {
for (let j = 0; j < сomments.length; j++) {
if (сomments[j].сomment_id = nested_comments[i].сomment_id) {
сomments[j]['nested comment'] = nested_comments[i];
}
}
}
console.log(сomments)
但我只显示了最后一条评论。
我需要结果:
{
{
сomment_id: 1,
product_id: 1,
...,
nested comment: {
{
nested_id: 1,
сomment_id: 1,
product_id: 1,
...
},
{
nested_id: 2,
сomment_id: 1,
product_id: 1,
...
},
}
},
{
сomment_id: 2,
product_id: 1,
...,
nested comment: {
}
}
}
也就是说,第一个评论有两个回复,第二个没有,依此类推,可能有 10 个回复。
1 个回答