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

106
Vistas
JS Reduce: sum specific position in a nested array of numbers

I want to reduce only a specific position in a nested number array

const array1 = [[1,2,3],[4,5,6],[7,8,9],[11,12,13]];

console.log(array1.reduce((prev, curr) => prev[2] + curr[2]));

The result is NaN, another solution?

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

0

1) You should use prev not prev[2] as

prev is of type number that you are returning not an array

2) If you are adding the number then it is better to take initial value as 0 as a second argument to the reduce method.

If you won't pass second argument, then reduce will take the array element at 0 index as the starting value i.e [1, 2, 3] and then if you add [1, 2, 3] + curr[2] then it will produce value as 1,2,36, which you are not expecting

const array1 = [
  [1, 2, 3],
  [4, 5, 6],
  [7, 8, 9],
  [11, 12, 13],
];

console.log(array1.reduce((prev, curr) => prev + curr[2], 0));

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