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
How can I calculate the length of an array of objects with reduce?

I have this simple function, and I am just trying to get the length of this array of objects. For instance I want to return 3 in that case below. I could use .length() but I want to explore more with the reduce method.

function getSum(data) {
  const totalNum = data.reduce((sum, a) => sum + a.id, 0);
  return totalNum
}

console.log(getSum([
  {id: 'ddd6929eac', isComplete: true},
  {id: 'a1dd9fbd0', isComplete: true},
  {id: 'afa8ee064', isComplete: false}
]))

Thank you very much :)

about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You could add one for each item in the array.

function getSum(data) {
    return data.reduce((sum, a) => sum + 1, 0);
}

const
    data = [{ id: 'ddd6929eac', isComplete: true }, { id: 'a1dd9fbd0', isComplete: true }, { id: 'afa8ee064', isComplete: false }];

console.log(getSum(data));

about 4 years ago · Juan Pablo Isaza Denunciar

0

Well the equivalent of data.length would be:

data.reduce(sum => sum + 1, 0);

But I don't see why you would do that unless you're trying to exclude blank values.

about 4 years ago · Juan Pablo Isaza Denunciar

0

Simply you can just add index:

const totalNum = data.reduce((sum, a, index) => index+1);
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