角2 - 在一个数组
问题描述:
我的“任何”类型的其内的多个对象的数组,因为这样应用到一个对象的函数:角2 - 在一个数组
//List of posts (array)
this.posts = [
//First Post (object)
{
userFirst: 'Teyah',
userLast: 'Tharpe',
postText: 'Good morning, everyone! Have a good day. :)',
//First Post's comments (array)
comments: [
//First Comment (object)
{
cFirstName: 'Jalen',
cLastName: 'Tharpe',
theirComment: 'Thank you'
},
//Second Comment (object)
{
cFirstName: 'Gerald',
cLastName: 'Matthews',
theirComment: 'Thank you! You do the same.'
}
]
},
//Second Post (object)
{
userFirst: 'Jordan',
userLast: 'Gibson',
postText: 'What is the move for today?',
comments: [
{
cFirstName: 'Joshua',
cLastName: 'Stewart',
theirComment: 'Party at my house!!!'
}
]
}
];
正如你可以看到,我有一个数组的帖子,其中包含对象,并在帖子对象中,我有一个评论列表。我试图将comment()
函数应用于其中一个对象。说,posts[0]
。有人会告诉我,如果这在Angular 2的范围内?如果是这样,请帮助。
如需更多代码/信息,请让我知道。
谢谢。
答
是的,这是在Angular的范围内,因为它在javascript范围内!您的问题的详细答案取决于您想要对comment()
函数执行的操作。我会尽量预测你可能想做的一些事情。
-
添加一个新的评论有一定职位的评论阵列
addComment(post, comment) { post.comments.push(comment); }
-
查找后根据其索引并添加注释
addCommentUsingIndex(postIndex, comment) { this.posts[postIndex].comments.push(comment); }
-
提取的征求意见数量给定的帖子
countComments(postIndex) { return this.posts[postIndex].comments.length; }
-
得到一定的职位
transformComments(postIndex) { return this.posts[postIndex].comments.map(comment => { return comment.cFirstName + ' ' + comment.cLastName }); }
的每个评论者是否是大致覆盖您的需求的全名的数组?
你想做什么? –
你是什么意思“应用评论()函数”? –