伙计们,我真的被困在这里,
我正在创建一个具有子评论功能的基本评论评分系统
正如您在评论数组中看到的那样,“子评论”条目是对象,我想使用它们创建数组,但我不知道如何,因为它们位于数组中。
我尝试过:
this.CommentsArray.forEach((item, i) => {
if([item]["subcomments"] != undefined) {
[item]["subcomments"].forEach((subitem, i) => {
subitem = this.subCommentsArray[i];
})
}
,但不起作用。有什么建议吗?
谢谢..
编辑:
我的数据结构是这样的:
"comments" : {
"d4894e47181e43b7a0fb8b7699" : {
"commentDate" : "2018-08-13T19:49:49.814Z",
"commentPhotoExistence" : false,
"commentRate" : "3",
"commentRateState" : false,
"commentTagState" : false,
"commentText" : "",
"commentUID" : "d4894e47181e43b7a0fb8b7699",
"commenterUID" : "17QYQbhmyQeIYocIZBwH8R7ojhg1",
"commenterUsername" : "Test2",
"subcomments" : {
"7c9f985d39334573b0211d18f1" : {
"subcommentDate" : "2018-09-01T11:57:10.523Z",
"subcommentText" : "EXAMPLE TEXT1",
"subcommentUID" : "7c9f985d39334573b0211d18f1",
"subcommenterUID" : "17QYQbhmyQeIYocIZBwH8R7ojhg1",
"subcommenterUsername" : "Test2"
},
"a1197fb32eae483ba8e910a748" : {
"subcommentDate" : "2018-09-01T11:29:57.514Z",
"subcommentText" : "EXAMPLE TEST 2",
"subcommentUID" : "a1197fb32eae483ba8e910a748",
"subcommenterUID" : "17QYQbhmyQeIYocIZBwH8R7ojhg1",
"subcommenterUsername" : "Test2"
},
"ad648a09a67c431c821bf8f290" : {
"subcommentDate" : "2018-09-01T11:57:01.452Z",
"subcommentText" : "example3",
"subcommentUID" : "ad648a09a67c431c821bf8f290",
"subcommenterUID" : "17QYQbhmyQeIYocIZBwH8R7ojhg1",
"subcommenterUsername" : "Test2"
},
"f7682639d4584642a18a9a4ad7" : {
"subcommentDate" : "2018-09-02T15:41:38.162Z",
"subcommentText" : "example4",
"subcommentUID" : "f7682639d4584642a18a9a4ad7",
"subcommenterUID" : "17QYQbhmyQeIYocIZBwH8R7ojhg1",
"subcommenterUsername" : "Test2"
}
}
}
这里的问题是子注释ID是随机添加的。通常,如果是数组,我可以使用.forEach来使用每个项目,但是子注释和子注释属性之间存在随机uid。所以我想要的就是创建一个包含它们的数组,数组会像;
0: { "subcommentDate" : "2018-09-01T11:57:01.452Z",
"subcommentText" : "example3",
"subcommentUID" : "ad648a09a67c431c821bf8f290",
"subcommenterUID" : "17QYQbhmyQeIYocIZBwH8R7ojhg1",
"subcommenterUsername" : "Test2"
}
1: {
"subcommentDate" : "2018-09-01T11:57:01.452Z",
"subcommentText" : "example3",
"subcommentUID" : "ad648a09a67c431c821bf8f290",
"subcommenterUID" : "17QYQbhmyQeIYocIZBwH8R7ojhg1",
"subcommenterUsername" : "Test2"
}
2: {
"subcommentDate" : "2018-09-01T11:57:01.452Z",
"subcommentText" : "example3",
"subcommentUID" : "ad648a09a67c431c821bf8f290",
"subcommenterUID" : "17QYQbhmyQeIYocIZBwH8R7ojhg1",
"subcommenterUsername" : "Test2"
}
等
答案 0 :(得分:0)
您的代码在变量i
上留下了阴影。
您正在使用的位于第一行this.CommentsArray.forEach((item, i) => {
,然后再次位于subcomments
。
this.CommentsArray.forEach((item, i) => {
if([item]["subcomments"] != undefined) {
[item]["subcomments"].forEach((subitem, z) => {
subitem = this.subCommentsArray[z];
})
}
});
但这不是真正的问题。您正在尝试迭代一个对象,而不是数组。结果将返回为;
{
"d48...669": {
"commentDate": '2019-08-13',
"subcomments": {
"7c9...8f1": {
},
"a11...748": {
}
}
}
}
您应该按对象的键进行迭代;
Object.keys(this.CommentsArray).forEach(commentId => {
const comment = this.CommentsArray[commentId];
Object.keys(comment['subcomments']).forEach(subCommentId => {
const subComment = comment['subcomments'][subCommentId];
)};
});