如何用angular中的另一个数组的子对象创建一个数组
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何用angular中的另一个数组的子对象创建一个数组相关的知识,希望对你有一定的参考价值。
嗨伙计们,我真的被困在这里,
我正在创建一个具有子评论功能的基本评论评级系统
正如你在注释中看到的那样>“子注释”条目是对象,我想用它们创建数组,但我不知道如何因为它们在一个数组中。
我试过这个:
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。通常如果它是Array我可以使用.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"
}
等等
答案
你的代码遮蔽了变量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];
)};
});
以上是关于如何用angular中的另一个数组的子对象创建一个数组的主要内容,如果未能解决你的问题,请参考以下文章
如何用mongoose更新这个文档中的数组中的对象中的数组里的值?
如何用 ViewPager 中的另一个片段替换 Android 片段?