Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

173
Visualizações
Return Multiple things in a map function - react/Javscript

What would be the most efficient way to return multiple things in a .map function. For example, 2 separate console.log statements?

For example: This works just fine:

 return (
    <div className="App">
      {mycataobjects.map((myobject) => console.log(myobject.name))}
    </div>
  );

However, how would I add another console.log to this map, so that for each object in the array I get the phrase "Hello"

I tried the below but it does not work. It only prints "Hello" once. I want it printed for every object in the my.object array. What would be the best way to do this?

 return (
    <div className="App">
      {mycataobjects.map(
        (myobject) => console.log(myobject.name),
        console.log("Hello")
      )}
    </div>
  );
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

If you are really interested in the return values you could return a tuple kind of type and then flatten the array:

const arr = mycataobjects.map(item => [1, 2]);
const values = arr.flat();

However in your example if you just want to call console.log twice you'd need to add braces to your arrow function body:

mycataobjects.map(
  (myobject) => {
    console.log(myobject.name);
    console.log("Hello");
  }
);

Note that console.log is a void function so would just return undefined. Trying to grab its return value is not all that useful here.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda