Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Get sum of duplicate obj in a nested array object

I have an obj array like this:

[
  {
    "feeTypeTitle": "STORAGE",
    "itemFees": [
      {
        "idx": 0,
        "itemTitle": "Apple iPhone 11 128GB",
        "itemFee": 4,
        "feeType": "STORAGE"
      },
      {
        "idx": 1,
        "itemTitle": "Sony FWD-40WD650/T",
        "itemFee": 4,
        "feeType": "STORAGE"
      },
      {
        "idx": 1,
        "itemTitle": "Sony FWD-40WD650/T",
        "itemFee": 2,
        "feeType": "STORAGE"
      }
    ]
  },
  {
    "feeTypeTitle": "VERIFICATION",
    "itemFees": [
      {
        "idx": 0,
        "itemTitle": "Apple iPhone 11 128GB",
        "itemFee": 5,
        "feeType": "VERIFICATION"
      },
      {
        "idx": 1,
        "itemTitle": "Sony FWD-40WD650/T",
        "itemFee": 5,
        "feeType": "VERIFICATION"
      }
    ]
  }
]

You can see in STORAGE, there are 2 items that same title, idx number. I want to merge and sum the itemFee of these duplicates and return same array structure. The result I want is:

[
  {
    "feeTypeTitle": "STORAGE",
    "itemFees": [
      {
        "idx": 0,
        "itemTitle": "Apple iPhone 11 128GB",
        "itemFee": 4,
        "feeType": "STORAGE"
      },
      {
        "idx": 1,
        "itemTitle": "Sony FWD-40WD650/T",
        "itemFee": 6, // <- sum value
        "feeType": "STORAGE"
      }
    ]
  },
  {
    "feeTypeTitle": "VERIFICATION",
    "itemFees": [
      {
        "idx": 0,
        "itemTitle": "Apple iPhone 11 128GB",
        "itemFee": 5,
        "feeType": "VERIFICATION"
      },
      {
        "idx": 1,
        "itemTitle": "Sony FWD-40WD650/T",
        "itemFee": 5,
        "feeType": "VERIFICATION"
      }
    ]
  }
]

How do I do this with vanilla js, ES6?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Thank you for help

>Solution :

You can do:

const data = [{feeTypeTitle: 'STORAGE',itemFees: [{idx: 0,itemTitle: 'Apple iPhone 11 128GB',itemFee: 4,feeType: 'STORAGE',},{idx: 1,itemTitle: 'Sony FWD-40WD650/T',itemFee: 4,feeType: 'STORAGE',},{idx: 1,itemTitle: 'Sony FWD-40WD650/T',itemFee: 2,feeType: 'STORAGE',},],},{feeTypeTitle: 'VERIFICATION',itemFees: [{idx: 0,itemTitle: 'Apple iPhone 11 128GB',itemFee: 5,feeType: 'VERIFICATION',},{idx: 1,itemTitle: 'Sony FWD-40WD650/T',itemFee: 5,feeType: 'VERIFICATION',},],},]

const result = data.map((o) => {
  const itemFeesHash = o.itemFees.reduce((a, c) => {
    a[c.idx] = a[c.idx] || { ...c, itemFee: 0 }
    a[c.idx].itemFee += c.itemFee
    return a
  }, {})
  o.itemFees = Object.values(itemFeesHash)
  return o
})

console.log(result)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading