我们可以添加两个或多个相同类型的对象吗?

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

我有 3 个相同类型的对象,它们的 属性 值不同。我想按如下方式将它们加在一起:

例如

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},
        ....
    }
}

以及我想要的结果

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

是否可以添加它们?

如果不是,您是否建议为此编写任何有效的代码,而不是为每个代码使用三种不同的类型?

EDIT:加法是指三个对象的 属性 值相加。虽然这些对象也有一些可以是字符串的属性,但我只对数字值感兴趣。

作为替代方案,这里有一个对任何级别的任何数字求和的方法

非数字值将具有最后处理的对象的值(如果需要可以更改)

这不能正确处理数组属性

const objA = {
    data: {
        num: 1,
        SH: {
            propertyA: 0,
            propertyB: 3,
            propertyC: 0
        },
        text: 'objA',
        x: {
          y: {
            a: 1,
            b: 2,
            c: 3
          }
        }
    }
};
const objB = {
    data: {
        num: 2,
        SH: {
            propertyA: 0,
            propertyB: 0,
            propertyC: 1
        },
        text: 'objB',
        x: {
          y: {
            b: 4
          }
        }
    }
};
const objC = {
    data: {
        SH: {
            propertyA: 4,
            propertyB: 0,
            propertyC: 0
        },
        text: 'hello world',
        x: {
          y: {
            a: 1
          }
        }
    }
};

const addObjects = (...objs) => objs.reduce((result, obj) => {
        const fn = (obj, dest = result) => {
            Object.entries(obj).forEach(([key, value]) => {
                if (typeof value === 'object') {
                    dest[key] = dest[key] || {};
                    fn(value, dest[key]);
                } else {
                    if (typeof value === 'number') {
                        dest[key] = (dest[key] || 0) + value;
                    } else {
                        dest[key] = value;
                    }
                }
            });
            return result;
        };
        return fn(obj, result);
    }, {}
);
console.log(addObjects(objA, objB, objC));

最后就是循环了很多。可以通过多种方式完成循环。最简单的方法 os 查看对象本身并在对象不存在时添加内容。

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);