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

159
Views
How to count elements in 2 different arrays in javascript with same index

I have 2 different arrays in javascript, they have always the same length and I need to count one element + another element from another array with same index

let arr1 = [1, 2, 3];
let arr2 = [8, 9, 2];

Expected Output 
1 + 8 = 9
2 + 9 = 11
3 + 2 = 5

let result = [9, 11, 5]

What would be the easiest and the most elegant way to do it? I tried for loop and map as well but I didn´t find working solution

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

can achieve this with just 1 map

let arr1 = [1, 2, 3];
let arr2 = [8, 9, 2];

const sums = arr1.map((e,index) => e+arr2[index])

console.log(sums)

or a simple for loop

let arr1 = [1, 2, 3];
let arr2 = [8, 9, 2];

const sums = []

for(let i=0; i<arr1.length; i++){
  sums.push(arr1[i]+arr2[i])
}

console.log(sums)

or with a forEach

let arr1 = [1, 2, 3];
let arr2 = [8, 9, 2];

const sums = []

arr1.forEach((e,index) => {
  sums.push(e+arr2[index])
})

console.log(sums)

or with reduce

let arr1 = [1, 2, 3];
let arr2 = [8, 9, 2];

const sums = arr1.reduce((acc,curr,index) => {
  acc.push(curr+arr2[index])
  return acc
},[])

console.log(sums)

about 4 years ago · Santiago Gelvez 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!