Home > Software design >  Sum in nested object for each id
Sum in nested object for each id

Time:10-31

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'
    },
}

CodePudding user response:

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; }
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>

  • Related