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

How to the get the sum of all the field with the same name in a nested array?

I’m trying to find the sum of all the field values of the nested array, I suck at explaining stuffs like this, I hope the code below will give you some insight of what I want to do.

The array looks like this

data = [
    {
        id: 8434,
        name: listName, 
        data: [
            {name: "a name",
             quantity: 20,
            }
            {name: "another name",
             quantity: 40,
            }
            {name: "another new name",
             quantity: 40,
            }
        ]
    }, 
    {
        id: 54343,
        name: "new list name", 
        data: [
            {name: "a name",
             quantity: 12,
            }
            {name: "another name",
             quantity: 10,
            }
            {name: "another new name",
             quantity: 16,
            }
        ]
    }
]

This is how I want the data to be after carrying out the sum

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

transformed = [
{name: "a name", quantity: 32},
{name: "another name", quantity: 50},
{name: "another new name", quantity: 56}
]

>Solution :

just loop them through and create a new object..

const data = [
    {
        id: 8434,
        name: 'listName', 
        data: [
            {
                name: "a name",
                quantity: 20,
            },
            {
                name: "another name",
                quantity: 40,
            },
            {
                name: "another new name",
                quantity: 40,
            }
        ]
    }, 
    {
        id: 54343,
        name: "new list name", 
        data: [
            {
                name: "a name",
                quantity: 12,
            },
            {
                name: "another name",
                quantity: 10,
            },
            {
                name: "another new name",
                quantity: 16,
            }
        ]
    }
]


// Logic:
const transformed = []

data.forEach(d => {
    d.data.forEach(item => {
        const exist = transformed.find(t => t.name == item.name)
        if(exist) 
            exist.quantity += item.quantity
        else 
            transformed.push(item)
    })
})

console.log(transformed)

Output:

[
  { name: 'a name', quantity: 32 },
  { name: 'another name', quantity: 50 },
  { name: 'another new name', quantity: 56 }
]
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