Passing parameters from array to array

-1

I need to search within 2 arrays according to the parameter passed by the first array, eg

I need to scan the "profiles" array by grabbing each id and searching the other two:

const profiles = [
    {
      id: 1,
      userID: '1',
      favoriteMovieID: '1',
    },
    {
      id: 2,
      userID: '2',
      favoriteMovieID: '1',
    },
    {
      id: 3,
      userID: '4',
      favoriteMovieID: '5',
    },
    {
      id: 4,
      userID: '5',
      favoriteMovieID: '2',
    },
    {
      id: 5,
      userID: '3',
      favoriteMovieID: '5',
    },
    {
      id: 6,
      userID: '6',
      favoriteMovieID: '4',
    },
  ];

const users = {
    1: {
      id: 1,
      name: 'Jane Cruz',
      userName: 'coder',
    },
    2: {
      id: 2,
      name: 'Matthew Johnson',
      userName: 'mpage',
    },
    3: {
      id: 3,
      name: 'Autumn Green',
      userName: 'user123',
    },
    4: {
      id: 4,
      name: 'John Doe',
      userName: 'user123',
    },
    5: {
      id: 5,
      name: 'Lauren Carlson',
      userName: 'user123',
    },
    6: {
      id: 6,
      name: 'Nicholas Lain',
      userName: 'user123',
    },
  };

  const movies = {
    1: {
      id: 1,
      name: 'Planet Earth 1',
    },
    2: {
      id: 2,
      name: 'Selma',
    },
    3: {
      id: 3,
      name: 'Million Dollar Baby',
    },
    4: {
      id: 4,
      name: 'Forrest Gump',
    },
    5: {
      id: 5,
      name: 'Get Out',
    },
  };

How can I do this?

    
asked by anonymous 01.10.2018 / 20:28

3 answers

0

Knowing that Arrays and Objects are very similar in JS, you can refer to a property of an object using [] and putting the name inside, and vice versa. Example:

var obj = {
   prop1 = 1,
   prop2 = 2
}
console.log(obj[prop1]); // "1"

And vice versa.

var array = ['um', 'dois', 'três']
console.log(array.2); // "três"
// Conforme apontado nos comentários, este codigo geraria um erro pois uma propriedade não pode iniciar em número, mas a lógica é basicamente essa.

After understanding this, it is easy to solve the problem:

profiles.forEach(function(item, index) { //Itera sobre a array profiles
  let nome = users[item.userID].name; //pega o nome da pessoa usando o userID
  let id = users[item.userID].id; //pega o ID da pessoa na tabela de users
  let filme = movies[item.favoriteMovieID].name; //pega o filme favorito usando o id

  console.log(nome + ', com id ' + id + ', assiste o filme ' + filme);
})

If you prefer to have the names in quotation marks, just add to the string and concatenate, like this:

console.log('"' +nome + '", com id "' + id + '", assiste o filme "' + filme + '"');
    
01.10.2018 / 21:18
0

Since the keys of the variables movies and users are the ids of the same, you can use profile.userID and profile.favoriteMovieID to search objects directly.

In the example below I run profiles adding information to object based on object id.

const profiles = [
    {
        id: 1,
        userID: '1',
        favoriteMovieID: '1',
    },
    {
        id: 2,
        userID: '2',
        favoriteMovieID: '1',
    },
    {
        id: 3,
        userID: '4',
        favoriteMovieID: '5',
    },
    {
        id: 4,
        userID: '5',
        favoriteMovieID: '2',
    },
    {
        id: 5,
        userID: '3',
        favoriteMovieID: '5',
    },
    {
        id: 6,
        userID: '6',
        favoriteMovieID: '4',
    },
];

const users = {
    1: {
        id: 1,
        name: 'Jane Cruz',
        userName: 'coder',
    },
    2: {
        id: 2,
        name: 'Matthew Johnson',
        userName: 'mpage',
    },
    3: {
        id: 3,
        name: 'Autumn Green',
        userName: 'user123',
    },
    4: {
        id: 4,
        name: 'John Doe',
        userName: 'user123',
    },
    5: {
        id: 5,
        name: 'Lauren Carlson',
        userName: 'user123',
    },
    6: {
        id: 6,
        name: 'Nicholas Lain',
        userName: 'user123',
    },
};

const movies = {
    1: {
        id: 1,
        name: 'Planet Earth 1',
    },
    2: {
        id: 2,
        name: 'Selma',
    },
    3: {
        id: 3,
        name: 'Million Dollar Baby',
    },
    4: {
        id: 4,
        name: 'Forrest Gump',
    },
    5: {
        id: 5,
        name: 'Get Out',
    },
};

for (let i=0, l=profiles.length ; i < l ; i++) {
    let profile = profiles[i];

    profile.user = users[profile.userID] || 'Usuário com id (${profile.userID}) não encontrado.';
    profile.favoriteMovie = movies[profile.favoriteMovieID] || 'Filme com id (${profile.userID}) não encontrado.';
    
    console.log(profile);
}
    
01.10.2018 / 21:16
0

Friend, I think this way you can recover the user and the movie that belongs to each profile:

profiles.forEach(profile => {
    
    let usuario = users.find( user => user.id == profile.userID );
    let filme = movies.find( movie => movie.id == profile.favoriteMovieID);

    console.log(usuario.name + ' assiste ' + filme.name);
});
    
02.10.2018 / 15:42