I have a posts array with comments and replies. I am trying to filter all replies where body contains 'first comment' and also select all the replies where post_id = 1 but I'm getting an error.
P.S. I don't know the index.
error: Uncaught TypeError: Cannot read property 'body' of undefined
Here is the posts array.
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'
}
]
}
]
}
]
I tried it like this but it is giving me errors:
/**
* 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
})