Skip to content
Advertisement

Can we do addition of two or more objects of same type?

I have 3 objects of the same type with different values of their property. I would like to add them together as follow :

For example

objA = { 
    data: {
        SH: { propertyA: 0, propertyB: 3, propertyC: 0},
        ....
    }
}
objB = { 
    data: {
        SH: { propertyA: 0, propertyB: 0, propertyC: 1},
        ....
    }
}
objC = { 
    data: {
        SH: { propertyA: 4, propertyB: 0, propertyC: 0},
        ....
    }
}

And what I want a result like

objC = { 
    data: {
        SH: { propertyA: 4, propertyB: 3, propertyC: 1},
        ...
    } 
}

is it possible to add them?

And if not, do you suggest any efficient way of coding for this instead of having three different types for each of them?

EDIT: By addition, I mean numerical addition of property values from the three objects. Though the objects also have some properties which could be string, I am only interested in number values.

Advertisement

Answer

In the end it is a lot of looping. How you do the looping can be done a bunch of ways. Easiest way os to look over the objects themselves and add things when they do not exist.

objA = { 
    data: {
        SH: { propertyA: 0, propertyB: 3, propertyC: 0, x: 'funky-chicken'},
        OP: { OO: 1, ZZ: 2 },
    }
}
objB = { 
    data: {
        SH: { propertyA: 0, propertyB: 0, propertyC: 1, x: 'funky-chicken'},
        OP: { OO: 1, YY: 100 },
    }
}
objC = { 
    data: {
        SH: { propertyA: 4, propertyB: 0, propertyC: 0},
        AA: { A: 1 },
    }
}

const result = [objA, objB, objC].reduce(({ data }, obj) => {
  const entries = Object.entries(obj.data);
  entries.forEach(([key, items]) => {
    if (!data[key]){
      data[key] = { ...items };
    } else {
      Object.entries(items).forEach(([item, value]) => {
        if(typeof value === 'number') {
          data[key][item] = ( data[key][item] || 0 ) + value;
        }
      });
    }
  });
  return { data };
}, { data: {} })
console.log(result);
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement