Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

129
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda