Clubbing both complex objects does not work: Javascript

為{幸葍}努か 提交于 2020-03-04 21:39:56

问题


I have an source object that has the following structure

var obj1 = {
          type: "type1",
          nested: {
                level1: [
                  { field: "field1", value: "val1"},
                  { field: "field2", value: "val2"},
                  {
                     level2: [
                                  {
                                    field: "abc",
                                    value: "11",
                                  },
                                  {
                                    field: "abc",
                                    value: "12",
                                  }
                             ]
                  }
                ]
          },
          in: 0,
          out: 20
};

Also there is an input object, based on which merging should happen

var obj2 = {
          type: "type1",
          nested: {
                level1: [
                  { field: "field1", value: "val1"},
                  { field: "field3", value: "val5" }
                ]
          },
          in: 0,
          out: 20
};

Based on the new object I need to merge and resultant should hold only the unique one's. In my case the object can go deep upto level 2. The only thing I need is a manipulation on "nested" object. If same keys are present then update the value, else just append it. Also content within "nested" of obj2 will always be there in obj1's "nested". If something of obj2's "nested" is not present in obj1's "nested" then delete that object. Test cases file attached inside sandbox

Output should look like:

result = {
              type: "type1",
              nested: {
                      level1: [
                                { field: "field1", value: "val1"},
                                { field: "field2", value: "val2"},
                                { field: "field3", value: "val5" },
                                {
                                 level2: 
                                     [
                                         {
                                           field: "abc",
                                           value: "11",
                                         },
                                         {
                                           field: "abc",
                                           value: "12",  
                                         }
                                      ]
                                    }
                                  ]
                             },
                            in: 0,
                            out: 20
    };

Method that I have tried:

const merged = [...new Set([...obj1.nested.level1, ...obj2.nested.level1])]

Sandbox: https://codesandbox.io/s/angry-liskov-e5m1m


回答1:


Just google "deep merge javascript" or "merge nested objects". Here is one example of what i found:

// Merge a `source` object to a `target` recursively
const merge = (target, source) => {
  // Iterate through `source` properties and if an `Object` set property to merge of `target` and `source` properties
  for (const key of Object.keys(source)) {
    if (source[key] instanceof Object) Object.assign(source[key], merge(target[key], source[key]))
  }

  // Join `target` and modified `source`
  Object.assign(target || {}, source)
  return target
}

console.log(merge(obj1, obj2));


来源:https://stackoverflow.com/questions/60223299/clubbing-both-complex-objects-does-not-work-javascript

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!