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

207
Visualizações
How to sum multiplication values from objects in an array?

if i have an array that looks like this :

    "orderItems": [
  {
    "description": "Test",
    "identifier": "Test identifier",
    "priceCent": 1,
    "lengthIn": 10,
    "widthIn": 5,
    "heightIn": 10,
    "weightLbs": 3,
    "quantity": 2
  },
{
    "description": "Test-2",
    "identifier": "Test identifier",
    "priceCent": 1,
    "lengthIn": 10,
    "widthIn": 5,
    "heightIn": 10,
    "weightLbs": 4,
    "quantity": 3
  }
]

How can I multiply weightLbs and quantity and sum those within the objects? For example:

3 * 2 = 6 4 * 3 = 12

and result total I want is = 18

This is what I have so far, but this only adds weightLbs

        return delivery.order.orderItems.reduce((prev, curr) => {
      return prev + (curr.totalWeightLbs || 0);
    }, 0);
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

orderItems.reduce((sum, currentItem) => { 
    return sum + (currentItem.weightLbs * currentItem.quantity); 
}, 0);

this will multiply the values and sum them in the reduce accumulator

about 4 years ago · Juan Pablo Isaza Relatório

0

const dummyArray = [
  {
    "description": "Test",
    "identifier": "Test identifier",
    "priceCent": 1,
    "lengthIn": 10,
    "widthIn": 5,
    "heightIn": 10,
    "weightLbs": 3,
    "quantity": 2
  },
{
    "description": "Test-2",
    "identifier": "Test identifier",
    "priceCent": 1,
    "lengthIn": 10,
    "widthIn": 5,
    "heightIn": 10,
    "weightLbs": 4,
    "quantity": 3
  }
];

let sum = 0;

for( i = 0; i < dummyArray.length; i++)
  sum += dummyArray[i].weightLbs * dummyArray[i].quantity

console.log(sum)

this might work

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