How to merge complex arrays of objects?
-
I've been struggling with this one all day. I want to deep merge two arrays that contain objects with nested arrays and objects of their own.
I have tried both recursive and non-recursive functions - HISE seems to be a bit weird with recursion, and recursive functions can't be inline.
Anyone already solved this problem?
Here is some simple example data, in my actual project the data is more complicated, but this would be a good start. - There is no guarantee in the real-world data that all objects will have the same keys (for example they might not all have an id).
reg data1 = [ {"id": 1, "name": "Alice", "attributes": {"age": 30, "skills": ["JS"]}}, {"id": 2, "name": "Bob", "attributes": {"age": 25, "skills": ["Python"]}} ]; reg data2 = [ {"id": 1, "name": "Alice", "attributes": {"age": 31, "skills": ["C++"]}, "UniqueValue": 123}, {"id": 3, "name": "Charlie", "attributes": {"age": 35, "skills": ["Go"]}} ];
-
so how do you want to merge them? Is Alice 31 or 30?
I would refrain from a recursive logic, looks like you have a fixed depth hierarchy that you can just handcode.
-
@Christoph-Hart 31, the values from the second array should overwrite those in the first array.
@Christoph-Hart said in How to merge complex arrays of objects?:
looks like you have a fixed depth hierarchy that you can just handcode.
In this example but that could be different in the real thing.