0

我有一个包含评论和回复的帖子数组。我正在尝试过滤正文包含“第一条评论”的所有回复,并选择所有 post_id = 1 但出现错误的回复。

PS我不知道索引。

错误:未捕获的类型错误:无法读取未定义的属性“正文”

这是帖子数组。

let posts = [
  {
    id: 1,
    body: 'First post',
    comments: [
      {
        id: 1,
        body: 'First post comment',
        post_id: 1,
        replies: [
          {
            id: 1,
            post_id: 1,
            comment_id: 1,
            body: 'Reply to first comment of first post'
          }
        ]
      }
    ]
  }
]

我这样尝试过,但它给了我错误:

/**
 * error: Uncaught TypeError: Cannot read property 'body' of undefined
 */
let first_comment = posts.map(item => {
  return item.comments.replies.body.contains('first comment')
})


/**
 * error: Uncaught TypeError: Cannot read property 'post_id' of undefined
 */
let first_post_comments = posts.map(item => {
  return item.comments.replies.post_id === 1
})
4

0 回答 0