1

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() ?

1
  • inside first for loop if (!value.actors.includes(actor))value.actors.push(actor) ? Commented Jun 7, 2022 at 17:53

3 Answers 3

2

As per the requirement what I understood is that there is an existing array of movies object and you want to assign a hero in those movies. We have to ignore if the passed hero name is already there for that movie else add that hero under that movie. If my understanding is correct, Here you go :

const movies = {
  'big' : {
    actors : ['Elizabeth Perkins', 'Robert Loggia']
  },
  'forrest gump' : {
    actors : ['Tom Hanks', 'Robin Wright', 'Gary Sinise']
  },
  'cast away' : {
    actors : ['Helen Hunt', 'Paul Sanchez']
  }
};

function updateMoviesList(obj, heroName) {
    Object.keys(obj).forEach(key => {
    if (!obj[key].actors.includes(heroName)) {
        obj[key].actors.push(heroName)
    }
  })
  return obj;
}

console.log(updateMoviesList(movies, 'Tom Hanks'));

Sign up to request clarification or add additional context in comments.

1 Comment

Assuming that we interpreted OPs requirements correctly, this is the best way to do it.
1

You can use Push()

Like this from docs

let sports = ['soccer', 'baseball']
let total = sports.push('football', 'swimming')

console.log(sports)  // ['soccer', 'baseball', 'football', 'swimming']
console.log(total.length)   // 4

To access array inside dictionary you have first to access it

movies['big']['actors'].push('New Actor')

For not be "hard coded", if you do this?

let actor = 'Tom Hanks'
for (const value of Object.values(newMovies)){
    for (const act of value.actors){
        if (value.actors.includes(actor)) { //Here you check if actor contains in array
            console.log("Ok, not add")
        }else{
            console.log("Here I have to add");
            value.actors.push(actor) //if not push to array          
            }
        }
    }

4 Comments

But I have an array as a value of a dictionary...
You have to access the array inside, like movies['big']['actors']. then you can use push like this movies['big']['actors'].push('New Actor')
but, I don't want to "hard code" so much my file
@ArielMarceloPardo see de edit if fits your needs
0
I was suggested to use map instead and the class Object...

function actorInMovies(movies, actor) {
    let mappedObject = { ...movies};
    Object.entries(movies).map(movie => {
        const movieName = movie[0];
        const actors = movie[1].actors;
        if (!actors.includes(actor))
          actors.push(actor);
        mappedObject = {
          ...mappedObject,
          [movieName]: { actors }
        };
      });
    return mappedObject
}

function main(){
    const movies = {
        'big' : {
            actors : ['Elizabeth Perkins', 'Robert Loggia']
        },
        'forrest gump' : {
            actors : ['Tom Hanks', 'Robin Wright', 'Gary Sinise']
        },
        'cast away' : {
            actors : ['Helen Hunt', 'Paul Sanchez']
        }
    };
    const actor = 'Tom Hanks';
    console.log(actorInMovies(movies, actor))
}

main();

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.