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

132
Vistas
Reduce method returns

Why does the second assignment need return and the first returns variable total automatically?

const total = inventors.reduce(((total, inventor) => total += inventor.passed - inventor.born), 0);
const transportation = data.reduce(((prevVal, currentVal) => {
      if (!prevVal[currentVal]) {
        prevVal[currentVal] = 0;
      }
      prevVal[currentVal]++;
      return prevVal;
    }), {})

If return prevVal is omitted I get Uncaught TypeError: Cannot read properties of undefined

What is the difference in how reduce() acts with values. Is it effected by ternary operation? If so then why this returns 0?

const transportation = data.reduce(((prevVal, currentVal) => 
(!prevVal[currentVal]) ? prevVal[currentVal] = 0 : prevVal[currentVal]++), {})
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

When using curly braces with an arrow function, the return is not implicit, so you have to use return

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