Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

122
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!