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

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

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 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