I am dealing with two variables in my code: parks
and review
.
let parks = [
{ id: 1, park_name: 'Average Park', review: [] },
{ id: 2, park_name: 'Better Park', review: [] },
{ id: 3, park_name: 'Cooler Park', review: [] },
{ id: 4, park_name: 'Dull Park', review: [] }
]
let review = [
{ id: 1, park_id: 1, comment: "It's an okay park. Seen better" },
{ id: 2, park_id: 1, comment: "Ehh, it's alright" },
{ id: 3, park_id: 2, comment: "It's pretty decent" },
{ id: 4, park_id: 3, comment: "Good not great" }
]
I want to add the objects from the review
variable to the parks.review
array based on matching park IDs.
[
{ id: 1, park_name: 'Average Park', review: [{ id: 1, park_id: 1, comment: "It's an okay park. Seen better" }, { id: 2, park_id: 1, comment: "Ehh, it's alright" }] },
{ id: 2, park_name: 'Better Park', review: [{ id: 3, park_id: 2, comment: "It's pretty decent" }] },
{ id: 3, park_name: 'Cooler Park', review: [{ id: 4, park_id: 3, comment: "Good not great" }] },
{ id: 4, park_name: 'Dull Park', review: [] }
]
What would be the most effective way to achieve this?