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

158
Views
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 answers
Answer question

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