Use map
with filter
:
let posts = [{
"id": 101,
"title": "Some post title"
},
{
"id": 102,
"title": "Some Another post title"
},
{
"id": 103,
"title": "Some Best post title ever"
}
]
let reviews = [{
"postId": 101,
"user": "Chris",
"comment": "Great post!"
},
{
"postId": 101,
"user": "Jason",
"comment": "Worth reading."
},
{
"postId": 102,
"user": "Dave",
"comment": "Waste of time"
}
]
const res = posts.map(({
id,
title
}) => ({
id,
title,
reviews: reviews.filter(({
postId
}) => postId == id).map(({
user,
comment
}) => ({
user,
comment
}))
}));
console.log(res);
0
solved need to create new array called result with key/value from posts and inside that reviews array with user and comment key/value objects