0

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

joy08
  • 9,004
  • 8
  • 38
  • 73
  • Does this answer your question? [How to deep merge instead of shallow merge?](https://stackoverflow.com/questions/27936772/how-to-deep-merge-instead-of-shallow-merge) – goto Feb 14 '20 at 09:25
  • your description is a bit vague. what means same keys? when should what be deleted? please add some use cases for keeping, adding and deleting. – Nina Scholz Feb 14 '20 at 11:05
  • @NinaScholz Inside the sandbox, there is a file for test cases – joy08 Feb 14 '20 at 11:11

1 Answers1

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));
Mischa
  • 1,591
  • 9
  • 14