I have two arrays like the following:
var directorAndFilms;
arrayOfFilmNames = [
'Heat',
'Summer Heat',
'City Heat',
'Heat',
'Red Heat',
'White Heat',
'Heat of Midnight',
'In the Heat of the Night: By Duty Bound',
'Tropical Heat',
'Delta Heat'
],
arrayOfDirectors = [
'Michael Mann',
'Monique van de Ven',
'Richard Benjamin',
'Jerry Jameson',
'Robert Collector',
'Raoul Walsh',
'Max Pécas',
'Harry Harris',
'Jag Mundhra',
'Michael Fischa'
]
I’m need to create an array of objects like the following:
[
{ name: 'Heat', director: 'Michael Mann' },
{ name: 'Summer Heat', director: 'Monique van de Ven' }
]
So far I’ve been able to do the following to achieve this:
var objOfFilmsAndDirectors = {};
arrayOfFilmNames.forEach(function(name, director) {
objOfFilmsAndDirectors[name] = arrayOfFilmDirectors[director];
});
directorAndFilms = Object.entries(objOfFilmsAndDirectors).map(
([name, director]) => ({name, director}));
This is giving me the following result, which is not quite right, since it’s putting the wrong director for the first film Heat
and there are no entries for Michael Mann
.
[
{ name: 'Heat', director: 'Jerry Jameson' },
{ name: 'Summer Heat', director: 'Monique van de Ven' },
{ name: 'City Heat', director: 'Richard Benjamin' },
{ name: 'Red Heat', director: 'Robert Collector' },
{ name: 'White Heat', director: 'Raoul Walsh' },
{ name: 'Heat of Midnight', director: 'Max Pécas' },
{
name: 'In the Heat of the Night: By Duty Bound',
director: 'Harry Harris'
},
{ name: 'Tropical Heat', director: 'Jag Mundhra' },
{ name: 'Delta Heat', director: 'Michael Fischa' }
]
How can I handle this? Or maybe there’s a better way? Any help is appreciated.
Thanks.