I have this kind of dictionary: INPUT
movies = {
'big' : {
actors : ['Elizabeth Perkins', 'Robert Loggia']
},
'forrest gump' : {
actors : ['Tom Hanks', 'Robin Wright', 'Gary Sinise']
},
'cast away' : {
actors : ['Helen Hunt', 'Paul Sanchez']
}
};
and I want to use this dictionary to get a different one. For example, I have to make a function called "moviesWithActors" that will received two arguments: "movies" and "actor". Actor could be "Tom Hanks", so when you find that he was on the movie, you don't add to the nested array, but if wasn't, you add.
OUTPUT
movies = {
'big' : {
actors : ['Elizabeth Perkins', 'Robert Loggia', 'Tom Hanks']
},
'forrest gump' : {
actors : ['Tom Hanks', 'Robin Wright', 'Gary Sinise']
},
'cast away' : {
actors : ['Helen Hunt', 'Paul Sanchez', 'Tom Hanks]
}
};
I do this:
for (const value of Object.values(newMovies)){
console.log(value.actors)
for (const act of value.actors){
//console.log(act)
if (act == actor) {
console.log("Ok, not add")
}else{
console.log("Here I have to add");
}
}
}
where "newMovies" is a copy of "movies" and "actor = "Tom Hanks" but I can't add to the array in actors: [ ... ]. Any suggestion? Can I use map() ?
if (!value.actors.includes(actor))value.actors.push(actor)?