I am working with two arrays of objects named films and filmDetails:
const films = [{
filmID: 0,
filmDetailsID: 9,
filmName: "Avatar",
price: 12
},
{
filmID: 1,
filmDetailsID: 10,
filmName: "Inception",
price: 18
},
{
filmID: 2,
filmDetailsID: 11,
filmName: "The Dark Knight",
price: 25
}
];
const filmDetails = [{
filmDetailsID: 10,
actor: "Leonardo DiCaprio",
fee: 150
},
{
filmDetailsID: 10,
actor: "Tom Hardy",
fee: 170
},
{
filmDetailsID: 11,
actor: "Christian Bale",
fee: 250
}
];
- I am looking to combine these two sets of objects by matching the filmDetailsID. I am considering using lodash for this task.
- In my final output, I want to select specific attributes from both films and film details. For films, I only want to include film name and price, while for film details, I need actor and fee.
Expected Result:
{
filmID: 0,
filmDetailsID: 9,
filmName: "Avatar",
price: 12
}, {
filmID: 1,
filmDetailsID: 10,
filmName: "Inception",
price: 15,
details: [{
filmDetailsID: 10,
actor: "Leonardo DiCaprio",
fee: 150
},
{
filmDetailsID: 10,
actor: "Tom Hardy",
fee: 170
}
]
}, {
filmID: 2,
filmDetailsID: 11,
filmName: "The Dark Knight",
price: 25,
details: [{
filmDetailsID: 11,
actor: "Christian Bale",
fee: 250
}]
}
My Attempt . However, my current approach merges the data without creating a separate 'details' object for adding matching film details IDs.
var mergedData = _.map(films, function(item){
return _.extend(item, _.findWhere(filmDetails, { filmDetailsID: item.filmDetailsID }));
});