在合并对象的键中创建值数组

Creating value arrays in keys of merged objects

与 javascript 我正在尝试比较嵌套 JSON 对象中的值。我想使用 Object.assign() 方法合并对象,但生成值数组。如果在其中一个对象中找不到 key/value 对,则数组应包含空值;

例如我有这 3 个输入对象:

let O1 = [
    { "group1": { "attr1": 1 } },
    { "group2": { "attr1": 2 } }
]

let O2 = [
    { "group2": { "attr1": 3 } },
    {
        "group3": {
            "attr1": 4,
            "attr2": 5
        }
    }
]

let O3 = [
    { "group1": { "attr1": 6 } },
    { "group3": { "attr1": 7 } }
]

我想将它们合并到这个新对象中:

let result = [
    { "group1": { "attr1": [1, null, 6] } },
    { "group2": { "attr1": [2, 3, null] } },
    {
        "group3": {
            "attr1": [null, 4, 7],
            "attr2": [null, 5, null]
        }
    }
]
  const result = [];

  for(var [index, array] of [O1, O2, O3].entries()){
     for(var [arrayIndex, obj] of array.entries()){
        (function copy(obj, res){
           for(var [key, value] of Object.entries(obj)){
              if(typeof value === "object"){
                 copy(value, res[key] || (res[key] = {}));
              } else {
                 if(!res[key]) res[key] = [];
                 res[key][index] = value;
             }
          }
        })(obj, result[arrayIndex] || (result[arrayIndex] = {}));
     }
  }

长话短说:更改您的数据结构...