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

127
Visualizações
Javascript: return statement inside .map array function gives undefined

Returns undefined. How ever if i console.log inside the map function that prints right value.

const myfun = (data) => {
    data.map((elem) => {
      return elem[1]  
    });
  };
  
  
let demo = [[1,2,3],[1,2,3]]
  
console.log(myfun(demo)); 
// gives undefined
// should give 2 2
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

Yes your function returns nothing, therefore undefined.

I think what you meant to write was this.

const myfun = (data) => (
    data.map((elem) => {
      return elem[1]  
    })
);

notice how the myfun function is not using curly braces instead it's using regular brackets.

or this.

const myfun = (data) => data.map((elem) => {
      return elem[1]  
    })

If you use curly brackets on an anonymous function you must explicitly use the return keyword.

about 4 years ago · Juan Pablo Isaza Relatório

0

This happens because you need to return the map, not just what is mapped

const myfun = (data) => {
    // You need to return the map
    return data.map((elem) => {
      return elem[1]  
    });
  };
  
  
let demo = [[1,2,3],[1,2,3]]
  
console.log(myfun(demo)); 

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