1

I am currently in the process of learning JavaScript and had a quick question on a project i am working on. Currently If i want to merge a few Objects, add the values together if the keys match or append the parent obj if a key is not already existing, i can do the following:

var test1 = {
    a: 12,
    b: 8,
    c: 17
};

var test2 = {
    a: 22,
    b: 8,
    c: 9
};

var test3 = {
    a: 33,
    b: 23,
    c: 1,
    d: 2,
    e: 9
};

function sumObjectsByKey(...objs) {
    return objs.reduce((a, b) => {
        for (let k in b) {
            if (b.hasOwnProperty(k))
                a[k] = (a[k] || 0) + b[k]
        }
        return a;
    }, {});
    console.log("endresult" + " " + sumObjectsByKey(test1, test2, test3));

This seems to work just fine when there is just ONE value. ex: a: 22

So this is where my issue comes in. What if the objects look like this:

var test1 = {
testSystem: {crit: "1", high: "0", med: "1", low: "22"}
testSystem1: {crit: "1", high: "0", med: "1", low: "22"}
testSystem2: {crit: "1", high: "0", med: "1", low: "22"}
testSystem3: {crit: "1", high: "0", med: "1", low: "22"}
};

var test2 = {
testSystem: {crit: "19", high: "305", med: "21", low: "212"}
4testSystem1: {crit: "111", high: "10", med: "31", low: "62"}
testSystem2: {crit: "21", high: "3", med: "11", low: "232"}
testSystem4: {crit: "13", high: "40", med: "15", low: "22"}
testSystem7: {crit: "21", high: "3", med: "112", low: "32"}
};

var test3 = {
testSystem5: {crit: "1", high: "0", med: "122", low: "122"}
testSystem2: {crit: "2", high: "6", med: "1", low: "222"}
testSystem3: {crit: "6", high: "0", med: "12", low: "212"}
testSystem4: {crit: "4", high: "8", med: "11", low: "2"}
};

how can i modify the above code to iterate through to do the same as above? Any explanation or help would be greatly appreciated. thanks.

3
  • 2
    What is your expected output? Commented Sep 5, 2019 at 6:52
  • Basically test2 and test3 would append test1 if a key does not already exist otherwise sum up the corresponding values. So for example if each var has a "testSystem2" you would add the crit,high,med,low values together. If there is no matching "testsystem" just append test1 obj with that the key and values. Commented Sep 5, 2019 at 6:59
  • Look into reducing an array of objects :) Commented Sep 5, 2019 at 6:59

1 Answer 1

2

You could check if the second object has a nested object, then call the function for the nested objects.

function sum(a, b) {
    Object.keys(b).forEach(k => {
        if (b[k] && typeof b[k] === 'object') return sum(a[k] = a[k] || {}, b[k]);
        a[k] = (+a[k] || 0) + +b[k];
    });
    return a;
}

var test1 = { testSystem: { crit: "1", high: "0", med: "1", low: "22" }, testSystem1: { crit: "1", high: "0", med: "1", low: "22" }, testSystem2: { crit: "1", high: "0", med: "1", low: "22" }, testSystem3: { crit: "1", high: "0", med: "1", low: "22" } },
    test2 = { testSystem: { crit: "19", high: "305", med: "21", low: "212" }, testSystem1: { crit: "111", high: "10", med: "31", low: "62" }, testSystem2: { crit: "21", high: "3", med: "11", low: "232" }, testSystem4: { crit: "13", high: "40", med: "15", low: "22" }, testSystem7: { crit: "21", high: "3", med: "112", low: "32" } },
    test3 = { testSystem5: { crit: "1", high: "0", med: "122", low: "122" }, testSystem2: { crit: "2", high: "6", med: "1", low: "222" }, testSystem3: { crit: "6", high: "0", med: "12", low: "212" }, testSystem4: { crit: "4", high: "8", med: "11", low: "2" } };
  
console.log([test1, test2, test3].reduce(sum));
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the quick response! I am curious though, why would I check if the second object has a nested object?
you like to mutate the first one, but only if the second has values to add, you need to check the properties of the second object.
On more question. What is the reason for the second plus sign in this: a[k] = (+a[k] || 0) + +b[k]; specificly I am talking about the '+ +b[k]'. Why is there two plus signs? Just curious and trying to understand.
it is an unary plus + which converts a string to a number.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.