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

Sum key values in array of objects where a key is the same

I have been playing with this a bit and I achieved a working function with one value, however I am trying to achieve summing multiple keys where a particular key is the same.

For example this is my data:

let data = [
    {
        name: 'John',
        value1: 1,
        value2: 2,
        value3: 3,
    },
    {
        name: 'Susan',
        value1: 1,
        value2: 2,
        value3: 3,
    },
    {
        name: 'John',
        value1: 1,
        value2: 2,
        value3: 3,
    },
    {
        name: 'Kate',
        value1: 1,
        value2: 2,
        value3: 3,
    },
    {
        name: 'Susan',
        value1: 1,
        value2: 2,
        value3: 3,
    },
];

And this would be the data after it has been processed:

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

data = [
    {
        name: 'John',
        value1: 2,
        value2: 4,
        value3: 6,
    },
    {
        name: 'Susan',
        value1: 2,
        value2: 4,
        value3: 6,
    },
    {
        name: 'Kate',
        value1: 1,
        value2: 2,
        value3: 3,
    },
]

The function I have for a single value is:

function sumValuesInArrayOfObjectsByKey(arr, key, value) {
    const map = new Map();
    for (const obj of arr) {
        const currSum = map.get(obj[key]) || 0;
        map.set(obj[key], currSum + obj[value]);
    }
    const res = Array.from(map, ([k, v]) => ({ [key]: k, [value]: v }));
    return res;
}

Any help or direction would be highly appreciated.

>Solution :

Don’t try to specify just a single key whose value you want to add up – iterate over all keys on the object other than name and add them to the matching value in the Map.

let data=[{name:"John",value1:1,value2:2,value3:3},{name:"Susan",value1:1,value2:2,value3:3},{name:"John",value1:1,value2:2,value3:3},{name:"Kate",value1:1,value2:2,value3:3},{name:"Susan",value1:1,value2:2,value3:3}];

function sumValuesInArrayOfObjectsByKey(arr, joiningKey) {
    const map = new Map();
    for (const { [joiningKey]: name, ...rest } of arr) {
        if (!map.has(name)) {
            map.set(name, { ...rest, [joiningKey]: name });
        } else {
            const mapObj = map.get(name);
            for (const [key, value] of Object.entries(rest)) {
                mapObj[key] = (mapObj[key] ?? 0) + value;
            }
        }
    }
    return [...map.values()];
}
console.log(sumValuesInArrayOfObjectsByKey(data, 'name'));
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