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

151
Visualizações
Sum in nested object for each id

i need to sum all the 'price' inside the product array attribute based on the id inside the 'fromSuppliers' attribute. What would be the best way to do that? I've tried with map and reduce but I wasn't successful. Any help will be appreciated.

the output expected for this example:

[  
  { id: 1, totalPrice: 19,84 }
  { id: 2, totalPrice: 9.84 }
]

the obj:

const Jsonresult = {
    items: {
      '**9089**': {
        createdAt: '2021-02-11T17:25:22.960-03:00',
        product: [{
            fromSuppliers: {
                productSupplier: {
                    stock: 102,
                    promoPrice: 16,
                },
                '**id**': 2
            }
        }],
        total: 9.84,
        quantity: 1,
        price: '**9.84**',
        updatedAt: '2021-02-11T17:25:22.960-03:00'
      },
      '**9090**': {
        createdAt: '2021-02-11T17:25:22.960-03:00',
        product: [{
            fromSuppliers: {
                productSupplier: {
                    stock: 102,
                    promoPrice: 7,
                },
                '**id**': 1
            }
        }],
        total: 9.84,
        quantity: 1,
        '**price**': 9.84,
        updatedAt: '2021-02-11T17:25:22.960-03:00'
      },
      '**9091**': {
        createdAt: '2021-02-11T17:25:22.960-03:00',
        product: [{
            fromSuppliers: {
                productSupplier: {
                    stock: 102,
                    promoPrice: 7,
                },
                '**id**': 1
            }
        }],
        total: 9.84,
        quantity: 1,
        '**price**': 10,
        updatedAt: '2021-02-11T17:25:22.960-03:00'
    },
}
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

You can use Object.values() to get an array of your items. Then we'd use Array.reduce() to create a map object, keyed on id. We'd use Object.values once more to turn our map object into an array.

const Jsonresult = { items: { '9089': { createdAt: '2021-02-11T17:25:22.960-03:00', product: [{ fromSuppliers: { productSupplier: { stock: 102, promoPrice: 16, }, id: 2 } }], total: 9.84, quantity: 1, price: 9.84, updatedAt: '2021-02-11T17:25:22.960-03:00' }, '9090': { createdAt: '2021-02-11T17:25:22.960-03:00', product: [{ fromSuppliers: { productSupplier: { stock: 102, promoPrice: 7, }, id: 1 } }], total: 9.84, quantity: 1, price: 9.84, updatedAt: '2021-02-11T17:25:22.960-03:00' }, '9091': { createdAt: '2021-02-11T17:25:22.960-03:00', product: [{ fromSuppliers: { productSupplier: { stock: 102, promoPrice: 7, }, id: 1 } }], total: 9.84, quantity: 1, price: 10, updatedAt: '2021-02-11T17:25:22.960-03:00' }, } };

const items = Object.values(Jsonresult.items);
const result = Object.values(items.reduce((acc, { price, product: [ { fromSuppliers: { id }} ]}) => {
    if (!acc[id]) acc[id] = { id, price: 0 };
    acc[id].price += price;
    return acc;
}, {}));

console.log(result)
.as-console-wrapper { max-height: 100% !important; top: 0; }

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