HISE Logo Forum
    • Categories
    • Register
    • Login

    How to merge complex arrays of objects?

    Scheduled Pinned Locked Moved Scripting
    arraymergerecursiveobjectcombine
    4 Posts 2 Posters 81 Views
    Loading More Posts
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes
    Reply
    • Reply as topic
    Log in to reply
    This topic has been deleted. Only users with topic management privileges can see it.
    • d.healeyD
      d.healey
      last edited by d.healey

      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"]}}
      ];
      

      Libre Wave - Freedom respecting instruments and effects
      My Patreon - HISE tutorials
      YouTube Channel - Public HISE tutorials

      Christoph HartC 1 Reply Last reply Reply Quote 0
      • Christoph HartC
        Christoph Hart @d.healey
        last edited by

        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.

        d.healeyD 1 Reply Last reply Reply Quote 1
        • d.healeyD
          d.healey @Christoph Hart
          last edited by

          @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.

          Libre Wave - Freedom respecting instruments and effects
          My Patreon - HISE tutorials
          YouTube Channel - Public HISE tutorials

          1 Reply Last reply Reply Quote 1
          • d.healeyD
            d.healey
            last edited by

            Slightly related, I've added an Object.keys function to the scripting API. Also works on arrays but will return an empty array in that case.

            Link Preview Image
            Added Object.keys by davidhealey · Pull Request #695 · christophhart/HISE

            favicon

            GitHub (github.com)

            Libre Wave - Freedom respecting instruments and effects
            My Patreon - HISE tutorials
            YouTube Channel - Public HISE tutorials

            1 Reply Last reply Reply Quote 1
            • First post
              Last post

            46

            Online

            1.7k

            Users

            11.7k

            Topics

            101.8k

            Posts