Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

149
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda