tnptop / array-object-merge

Merge objects with arrays of objects based on given object key
MIT License
0 stars 1 forks source link

Recursive merging #9

Open Migushthe2nd opened 4 years ago

Migushthe2nd commented 4 years ago

I'm trying to merge the second json (update) with the first (original):

{
    "Files": [
      {
        "FileName": "blyt/PslSelectSinglePlayer.bflyt",
        "Patches": [
          {
            "PaneName": "N_Cnt",
            "Position": {
              "X": 0,
              "Y": -200,
              "Z": 0
            }
          },
          {
            "PaneName": "RootPane",
            "Scale": {
              "X": 0.9,
              "Y": 0.9
            }
          }
        ]
      }
    ]
  }

and update with

{
    "Files": [
      {
        "FileName": "blyt/PslSelectSinglePlayer.bflyt",
        "Patches": [
          {
            "PaneName": "N_Cnt",
            "Position": {
              "X": 0,
              "Y": 0,
              "Z": 0
            }
          }
        ]
      }
    ]
  }

But the result is

{
  "Files": [
    {
      "FileName": "blyt/PslSelectSinglePlayer.bflyt",
      "Patches": [
        {
          "PaneName": "N_Cnt",
          "Position": {
            "X": 0,
            "Y": 0,
            "Z": 0
          }
        }
      ]
    }
  ]
}

while I need it to be

{
  "Files": [
    {
      "FileName": "blyt/PslSelectSinglePlayer.bflyt",
      "Patches": [
        {
          "PaneName": "N_Cnt",
          "Position": {
            "X": 0,
            "Y": 0,
            "Z": 0
          }
        },
        {
          "PaneName": "RootPane",
          "Scale": {
            "X": 0.9,
            "Y": 0.9
          }
        }
      ]
    }
  ]
}

Would it be possible to add an option for this? (Was this behaviour removed in https://github.com/tnptop/array-object-merge/issues/7 ?)

Edit: My code:

merge(original, update, [
  'FileName',
  'PaneName'
])
tnptop commented 4 years ago

To be honest, I didn't take a nested object into account, so as of this comment what the code actually does is just merge the immediate child of the object's properties. Which means this is an oversight in design, not related to #7.

Recursive merging has to be done inside-out, so it'll take a while to properly traverse and ensure the integrity of the object structure. In the worst case there might be a need to re-implement from scratch.

I'll keep you update on the status. Thank you very much for reporting this!