HISE Logo Forum
    • Categories
    • Register
    • Login

    Array.concat()

    Scheduled Pinned Locked Moved Scripting
    6 Posts 3 Posters 198 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.
    • CasmatC
      Casmat
      last edited by Casmat

      Hey!

      How do I use the concat() function?

      const var tagArray = [["Pad"], ["Chord", "String"], ["Pluck"]]
      

      Trying to make ^this^ turn into this:

      ["Pad", "Chord", "String", "Pluck"]
      

      Thanks!

      i make music

      d.healeyD ulrikU 2 Replies Last reply Reply Quote 0
      • d.healeyD
        d.healey @Casmat
        last edited by d.healey

        @Casmat Array concat is used for combining two arrays, so that won't do what you want.

        You need to flatten the array. I don't think there is a clean way to do it in HISE (with JS we'd usually use the spread or apply commands). So here is a dirty way using a recursive function.

        const var tagArray = [["Pad"], ["Chord", "String"], ["Pluck"]]
        
        function flattenArray(arr)
        {
        	var result = [];
        
        	for (i = 0; i < arr.length; i++)
        	{
            	    if (Array.isArray(arr[i]))
            	    {
              		var flattenedSubArray = flattenArray(arr[i]);
              	
              		for (j = 0; j < flattenedSubArray.length; j++)
                		result.push(flattenedSubArray[j]);
            	    }
            	    else
            	    {
              		result.push(arr[i]);
        	    }
        	}
        	
        	return result;
        }
        
        const newArray = flattenArray(tagArray);
        Console.print(trace(newArray));
        

        You might be able to get it a bit neater using the map function but I haven't explored it for this purpose.

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

        CasmatC 1 Reply Last reply Reply Quote 0
        • CasmatC
          Casmat @d.healey
          last edited by

          @d-healey ahh, makes sense! Thanks!

          i make music

          1 Reply Last reply Reply Quote 0
          • ulrikU
            ulrik @Casmat
            last edited by

            @Casmat you can do like this

            const var tagArray = [["Pad"], ["Chord", "String"], ["Pluck"]];
            
            const result = [];
            
            for (i = 0; i < tagArray.length; i++)
            	result.concat(tagArray[i]);
            
            Console.print(trace(result));
            
            Interface: [
              "Pad",
              "Chord",
              "String",
              "Pluck"
            ]
            

            Hise Develop branch
            MacOs 15.3.1, Xcode 16.2
            http://musikboden.se

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

              @ulrik Oh that puts my over-engineered method to shame :D

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

              CasmatC 1 Reply Last reply Reply Quote 0
              • CasmatC
                Casmat @d.healey
                last edited by

                @d-healey @ulrik amazing haha!

                i make music

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

                61

                Online

                1.7k

                Users

                11.7k

                Topics

                102.1k

                Posts