Passing array parameters to array

Asked

Viewed 50 times

-1

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

I need to scan the "profiles" array by picking 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 that ?

  • Friend, there is only one array, and it is the profiles array. The other two are objects (although objects and arrays are basically the same in JS). Anyway, can you specify better what you want? How do you want the result to come out?

  • I need you to leave "Jane Cruz", with id "1", watch the movie "Planet Earth 1"

3 answers

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);
});

0

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

In the example below I walk profiles adding the information to the object based on the 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);
}

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 in, 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 quote names, just add to the string and concatenate, like this:

console.log('"' +nome + '", com id "' + id + '", assiste o filme "' + filme + '"');
  • Only one correction: console.log(array.2); // "dois" would be // três. But you can’t have an identifier that starts with numbers, so this code will give an error. it would have to be array[2]

  • Well aimed, I will edit. Thank you

Browser other questions tagged

You are not signed in. Login or sign up in order to post.