How to look for a value within an arrangement

Asked

Viewed 44 times

0

I need to write a function that gives me a list of the name skills in the console input.log ms can’t understand this for-of. I’m a beginner in javascript and since yesterday I’m jumping from article to article but I can’t do.

var usuarios = [
 {
 nome: "Diego",
 habilidades: ["Javascript", "ReactJS", "Redux"]
 },
 {
 nome: "Gabriel",
 habilidades: ["VueJS", "Ruby on Rails", "Elixir"]
 }
];

function funcao(nome1, nome2) {
        for (nome1 of usuarios) {
                return usuarios.habilidades.join();
        for (nome2 of usuarios) {
                return usuarios.habilidades.join();
        };
        };
};

console.log(funcao('Diego', 'Gabriel'));
  • You want me to return 2 arrays or one with all the same abilities?

  • What is the expected output for the program?

  • You would know how to do using one for conventional?

  • I want you to return the two lists of skills

  • if you’re talking about the looping is, I don’t know how it could work

  • this is an exercise so I would need to do with for-of to learn how it works

Show 1 more comment

1 answer

1

A loop for will always be stopped when you call the return. When you have an input and you want to turn it into something else, you’re mapping it. That’s what you’re looking for here, mapping names with your skill array.

You must use the map and within each iteration search for the user’s object, and return their skills. Another practical tip is to use a spread in arguments, so logic will work the same way regardless of the number of arguments (names) that passes to this function.

You can do it like this:

var usuarios = [{
    nome: "Diego",
    habilidades: ["Javascript", "ReactJS", "Redux"]
  },
  {
    nome: "Gabriel",
    habilidades: ["VueJS", "Ruby on Rails", "Elixir"]
  }
];

function funcao(...nomes) {
  return nomes.map(nome => {
    const usuario = usuarios.find(obj => obj.nome === nome);
    return usuario ? usuario.habilidades : [];
  });
};

console.log(funcao('Diego', 'Gabriel'));

To do this with a for...of you need more code, but it could be like this:

var usuarios = [{
    nome: "Diego",
    habilidades: ["Javascript", "ReactJS", "Redux"]
  },
  {
    nome: "Gabriel",
    habilidades: ["VueJS", "Ruby on Rails", "Elixir"]
  }
];

function funcao(...nomes) {
  const habilidades = [];
  for (let nome of nomes) {
    const usuario = usuarios.find(obj => obj.nome === nome);
    habilidades.push(usuario ? usuario.habilidades : []);
  }
  return habilidades;
};

console.log(funcao('Diego', 'Gabriel'));

Browser other questions tagged

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