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

109
Visualizações
Javascript Multiplying Within an Array

I have an array with multiple arrays inside. I would like to multiply the contents of each array.

Here is my code--currently not getting anything in console. The desired output is 3 numbers-- the result of multiplication of the three arrays

var arr = [
  [1, 2, 3],
  [3, 4],
  [7, 8, 9]
]
var mult = 1
for (i = 0; i < arr.length; i++) {
  for (j = 0; j < arr.length; j++) {
    mult *= arr[i][j]
  }
  console.log(mult)
}

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

You could map the result of the nested multiplying.

const
    multiply = (a, b) => a * b,
    array = [[1, 2, 3], [3, 4], [7, 8, 9]],
    result = array.map(a => a.reduce(multiply, 1));

console.log(result);

about 4 years ago · Juan Pablo Isaza Relatório

0

You should iterate over rows. So you need to change inner loop on this :

for (j = 0; j < arr[i].length; j++)

const array = [
  [1, 2, 3],
  [3, 4],
  [7, 8, 9]
];

for (i = 0; i < array.length; i++) {
let result = 1;
  for (j = 0; j < array[i].length; j++) {
    result *= array[i][j]
  }
  console.log(result);
}

about 4 years ago · Juan Pablo Isaza Relatório

0

You can try this, it is easier using map and reduce functions. arr.map(array => array.reduce(arrayMult, 1)) is the multiplication of each inner array, like [ 6, 12, 504 ], and the last reduce is to multiply these values.

var arr = [[1, 2, 3],[3, 4],[7, 8, 9]];
const arrayMult = (prev, curr) => prev * curr;
const total = arr.map(array => array.reduce(arrayMult, 1)).reduce(arrayMult, 1);
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