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

123
Vistas
How to add to a value to a specific 2 dimensional array

I was wondering the best way to do this in javascript (ES6).

So I have a 2 dimensional array like so:

const two_dimensional_array = [
   [33000, 100],
   [33500, 200],
]

I now have another 2 dimensional array like so:

const another_array = [
 [33500, 1300]
]

and I want it to find the the first value from another_array in two_dimensional_array and then add the 2nd value to it. So the end result of two_dimensional_array would be like:

const two_dimensional_array = [
   [33000, 100],
   [33500, 1500],
]
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

  • Create a Map from another_array where the key is the first number of the pair and the value is the second number.
  • Using Array#forEach, iterate over two_dimensional_array and try to check if there's a number to add (0 otherwise).

const 
  two_dimensional_array = [ [33000, 100], [33500, 200] ],
  another_array = [ [33500, 1300] ];

const map = new Map(another_array);

two_dimensional_array.forEach(arr => arr[1] += map.get(arr[0]) ?? 0);

console.log(two_dimensional_array);

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