如何在ES6中显示对象的内容

时间:2017-11-06 16:56:05

标签: angularjs ecmascript-6

filterComment打印[object Object],[object Object] 如何显示对象的内容。

 var post = {id: 4, title: 'Post Title 4'};
var comments = [
  {postId: 4, content:'Comment for post id 4'},
  {postId: 3, content:'Comment for post id 3'},
  {postId: 1, content:'Comment for post id 1'},
  {postId: 4, content:'Another Comment for post id 4'}
 ];

function commentsForPost(singlePost, allComments)   {
  return allComments.filter(function(comment) {
    console.log('comment Id ' + comment.postId);
    console.log('Post Id ' + singlePost.id);
    return comment.postId === singlePost.id;
  });
}
var filterComment = commentsForPost(post, comments);
`Filter comment ${filterComment}` //console.log this line prints [object Object],[object Object] 

我期待filterComment的内容。即[{“postId”:4,“content”:“对帖子ID 4的评论”},{“postId”:4,“content”:“post id 4的另一个评论”}}

感谢。

1 个答案:

答案 0 :(得分:0)

这个怎么样? :

allComments
.filter(function(comment) {
  console.log('comment Id ' + comment.postId);
  console.log('Post Id ' + singlePost.id);
  return comment.postId === singlePost.id;
})
.map(comment => comment.content);

或使用ES6 destructuring,只需:

.map({ content } => content);