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

148
Visualizações
Why does this function return undefined but console.logs the correct answer?

I have this function

 const getMonthlyPriceFromData = (planName) => {
    planTypeData.map((item) => {
      if (item.name === planName) {
console.log(item.monthlyFee, 'fee')
        return item.monthlyFee;
      }
      return null;
    });
  };

when I console.log(item.monthyFee) it returns the correct answer but when I call

console.log(getMonthlyPriceFromData('Free')) it returns undefined?

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

There is no actual return statement inside your function:

  const getMonthlyPriceFromData = (planName) => {
    return planTypeData.map((item) => {
      if (item.name === planName) {
console.log(item.monthlyFee, 'fee')
        return item.monthlyFee;
      }
      return null;
    });
  };

Or use short arrow form. This way you can omit the return keyword, only if you skip the curly braces too {:

  const getMonthlyPriceFromData = (planName) => planTypeData.map((item) => {
      if (item.name === planName) {
console.log(item.monthlyFee, 'fee')
        return item.monthlyFee;
      }
      return null;
    })

EDIT:

OP seems to want only one item retuned from the array and for that find() would be a better approach:

const getMonthlyPriceFromData = (planName) => planTypeData.find((item) => {       
if (item.name === planName) { console.log(item.monthlyFee, 'fee')         
return true;       }       
return false;     })
about 4 years ago · Juan Pablo Isaza Relatório

0

You need to add a "return" before "planTypeData.map", like this:

 const getMonthlyPriceFromData = (planName) => {
     return planTypeData.map((item) => {
         if (item.name === planName) {
             return item.monthlyFee;
         }
         return null;
     });
 };


 console.log(getMonthlyPriceFromData('Free'))

Then it should work!

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