HISE Logo Forum
    • Categories
    • Register
    • Login

    How do I find and remove objects in an array?

    Scheduled Pinned Locked Moved Scripting
    23 Posts 2 Posters 381 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.
    • VirtualVirginV
      VirtualVirgin
      last edited by

      I can't seem to get any matches with this syntax:

      Screenshot 2025-01-28 at 1.59.05 PM.png

      You can listen to my orchestral mockups here:
      https://www.virtualvirgin.net/

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

        @VirtualVirgin The problem is each time you write {} you are creating a new object. So in your indexOf and remove functions you are creating new objects.

        Instead you need to reuse existing instances of those objects.

        const obj1 = {a: 1, b: 2};
        const obj2 = {c: 3, d: 4};
        
        const arr = [obj1, obj2];
        
        Console.print(arr.indexOf(obj2)); // 1
        

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

        VirtualVirginV 1 Reply Last reply Reply Quote 0
        • VirtualVirginV
          VirtualVirgin @d.healey
          last edited by

          @d-healey
          Yikes! I have a mess then. I have to name every object inside the array by declaring a variable for it?

          You can listen to my orchestral mockups here:
          https://www.virtualvirgin.net/

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

            @VirtualVirgin said in How do I find and remove objects in an array?:

            I have to name every object inside the array by declaring a variable for it?

            Nope. That's just the way of explaining why you were having the issue you described. There are more practical ways to do this kind of thing but the best approach will depend on exactly what you need to do and the context in which you're doing it. I need more info.

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

            VirtualVirginV 1 Reply Last reply Reply Quote 0
            • VirtualVirginV
              VirtualVirgin @d.healey
              last edited by

              @d-healey I'm working on a primitive piano roll for MIDI note editing and having trouble with note selection.
              To try this out, just double-click on an empty cell to create a note, and double-click again to destroy it.
              I think I fixed the adding and removing of array objects.

              What I am working on now is trying to get the selection methods to work properly for each condition. If I select a note by clicking, that is working, but my click-drag selection is not (even though I can see it is populating my "selectedNotes" array properly).

              The issue seems to be with my "isSelected" inline function, which is working for the click selection, but not the drag selection.

              After that, I need to fix the selection drag to move the notes, as it was working but I broke it somehow.

              HiseSnippet 2814.3ocwZrsaabbcorYfW1Fzjh9VeYrApAYjLMorbrsrkijokufXYKH4KMPPHXztCIGqk6Pr6RKs1guTf7GzW6+R9TxmPetujdNyrWlYuPIKjhRXXwYly4Lm6myLC2MP3vBCEAVMZ853oLqF+wl6G6GMdvXJ2254O1pwsatC8Xt+HxtbpufrmvyibCxDp+LpG4DQvwjgh.RHyi4DwE9DOwHti0ihmRCCYtVMZbomhjpg8ksje92e2indTeGV9TVVuUvcXufOgGkO6ta98bOumPcYulOQC50174NB+ABOwLfsuTydVSoNGSGwdIEAaolVOiFN1pw2zzwYs6wt6vUuSuUuG61tqczp86QWs28t2cXza2y4n6wnqN7HmdVM9hsc4Qhf8inQrPqFW9QB238GKNwWsAukGxOxigC5asOrypoehvyEEQbVqAi4dt6lpOCs.prat18RJs6eo4NbWd174Z4uRt.IGCcEXikLYuKYvd80YudZrWErTCMV5xJV5qatuS.eZT9JH+7GZ9b+HVvPJXmzYEErVK8e9qMu4MSbIBPWhVsFH.L7i5NgdL6IAvfLJzteud8Vgbmd85b+Vs.iWXD4Cz.xTpOyirAIEyQrnAhISE9vf1WaWb09WCQA1pWJhXjPPI.F5VHx9vDCXddg.AN3v6S.X1JHfFShDR3XjOAb1IqPbDdj4DwQuGbPCkNqHpgRh3.DXe9GY.M9DBX35j0VAQC9R+UuKYtjtOMf6RBAvj33BrvHLfXCxPpWHSBxiSmLDsQYvowfx4TgILWTZzX7zo0DJoH3RnglhQFke0vggrHIee55DP6FC+eB+lrFJpoLq1tCAofiT.hq+LOOMF.CeCf+R8G4gJa.HotWZI.qE2OB3kYQbeVKosqKrM6hSumZ11Cm4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INdODsKK.3G.FuxK1ofRYtYGxKNOo9ajL.aoYgETxyTV5MhNibnxpklex7ID9nW0q7aKOkKbZeRwkFNrx0vmUCeaNvAqpkwWUMXQH11eF1o3JfODd8ho.Z03xlOGYy5eNR8WK0Q8JeZ.J7etOO5USY908FpVIOMH9zkIbE.Zj7gK+yIObIy8sb1ISEAQVbW7kKSG12Rx35uyr0ad9ioQzTxATF1so3c4gBRiGy9.2godWT6lOlEdbjXpD1jWkzpwUN2a9o4a7OrYrNWHy4lMwlebSUrY1L+5OuIOhMI8Its+h+0wO62vE9.P7cEgbzN8C5OodUr3Wl9vtXAAI2cklp2TUi090e9WxYMx+3WLYMq3hrl0+bSpmm3jzai.zZ+olaAmpOehoz.fMz3GckhU4Gf9qZtivclGMx78vwe0.IK.RqwiPiOrrOnEhMTAKsn2vu1GIewug+4kc+5l6xibFWM+tTE7K3A9+Z9M4mbvW1b6gCgTU4L6ka9j+9B+8E76CqndS3QPqpAbvgq4KmMYeve1gAbhO3FhYNZrDFiqF2CGiZl8Y9txA+F7IYw933FIK1OcQqITm.wO5nxLf+.GthbFfm7k+XPratCNlTQpfIbW9O53XRpRHt5EEwacQQbsKJh29hh32dQQ7NWTDu6YiH9ygYqYQhIpvIKqc1caYp6FM11mBdiROWq+KPJw4zT
              

              You can listen to my orchestral mockups here:
              https://www.virtualvirgin.net/

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

                @VirtualVirgin said in How do I find and remove objects in an array?:

                I'm working on a primitive piano roll

                Have you seen the built in piano roll with the MIDI player? I think it might just be a viewer, I haven't played around with it. Lots of docs though.

                a5d3382a-c6d3-438d-9f75-0be155eaf75d-image.png

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

                VirtualVirginV 1 Reply Last reply Reply Quote 0
                • VirtualVirginV
                  VirtualVirgin @d.healey
                  last edited by

                  @d-healey I did take a look at it, but I don't see any way to edit the notes from the viewer. I just think it outputs the graphics from a MIDI file, so I though it was just as well if I try to build my own.

                  You can listen to my orchestral mockups here:
                  https://www.virtualvirgin.net/

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

                    @VirtualVirgin Yeah I think you're right about the viewer. But you can use the player as the backend to manage the tracks, notes, etc. and build your own UI to display and edit.

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

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

                      For the original problem you might be able to make use of some of the fancier array functions like map, filter, find, some, etc. Or just use a good ol' fashioned loop to manually search the array for the object you're interested in.

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

                      VirtualVirginV 1 Reply Last reply Reply Quote 0
                      • VirtualVirginV
                        VirtualVirgin @d.healey
                        last edited by

                        @d-healey said in How do I find and remove objects in an array?:

                        For the original problem you might be able to make use of some of the fancier array functions like map, filter, find, some, etc. Or just use a good ol' fashioned loop to manually search the array for the object you're interested in.

                        I am using filter and map in this script, but I am new to them so I may not be using them correctly.

                        My trouble at the moment is detecting which notes are selected

                        "selectedNotes" is an array that is getting populated properly to tell me which noteCells are selected,
                        but then I run this inline function to check and this only works somethimes:

                        // check for selected notes
                        inline function isSelected(cell)
                        {
                        	local sRow = selectedNotes.some(obj => obj.row === cell.row);
                        	local sCol = selectedNotes.some(obj => obj.col === cell.col);
                        	if (sRow && sCol)
                        		return true;
                        	else
                        		return false;
                        };
                        

                        I am trying to use that like "array.contains()", but in this case for specific objects ("cell").
                        The "cell" is an object with the coordinates i.e. {"row" : 60, "col" : 4}
                        This works when I use click or shift click to select my notes, but not when a drag selection has been made. Not sure why as they both populate the selectedNotes array.

                        You can listen to my orchestral mockups here:
                        https://www.virtualvirgin.net/

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

                          @VirtualVirgin For those functions (some, filter, etc.) the first parameter should be a function.

                          8ecde93d-8e0d-4864-9392-6862b29b2606-image.png

                          Edit: I just realised you're doing the javascript arrow function thing, we don't have that in HISE.

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

                          VirtualVirginV 1 Reply Last reply Reply Quote 0
                          • VirtualVirginV
                            VirtualVirgin @d.healey
                            last edited by

                            @d-healey said in How do I find and remove objects in an array?:

                            @VirtualVirgin For those functions (some, filter, etc.) the first parameter should be a function.

                            8ecde93d-8e0d-4864-9392-6862b29b2606-image.png

                            Edit: I just realised you're doing the javascript arrow function thing, we don't have that in HISE.

                            No, the arrow function definitely works:

                            Screenshot 2025-01-28 at 5.00.15 PM.png

                            You can listen to my orchestral mockups here:
                            https://www.virtualvirgin.net/

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

                              @VirtualVirgin

                              Oh that's good to know, thanks!

                              Now I can do this kind of thing:

                              Engine.showYesNoWindow("A title", "Some text", response => {
                              	Console.print(response);
                              });
                              

                              You could replace this

                              	if (sRow && sCol)
                              		return true;
                              	else
                              		return false;
                              

                              With return sRow && sCol

                              But this doesn't solve your issue. Can you make a simple snippet that demonstrates the issue?

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

                              VirtualVirginV 1 Reply Last reply Reply Quote 0
                              • VirtualVirginV
                                VirtualVirgin @d.healey
                                last edited by VirtualVirgin

                                @d-healey
                                I found where the problem is taking place:

                                            selectedNotes.push
                                            (
                                	            selectedNotes.filter(function (c) 
                                	            {
                                	                var cx = c.col * cellWidth;
                                	                var cy = height - (c.row + 1) * cellHeight;
                                	
                                	                return 
                                	                (
                                	                    cx + cellWidth > x1 &&
                                	                    cx < x2 &&
                                	                    cy + cellHeight > y1 &&
                                	                    cy < y2
                                	                );
                                	            })
                                            );
                                

                                This selectedNotes.push is pushing an object inside an array to the selectedNotes array, whereas I only need it to push an object to the array.
                                So the current result is looking like [[{"row":42, "col":4}]]
                                but I need it to just look like [{"row":42, "col":4}]
                                I am not sure how I modify that to just use a the object and not have it wrapped in that extra array.

                                You can listen to my orchestral mockups here:
                                https://www.virtualvirgin.net/

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

                                  @VirtualVirgin Add [0] after the parenthesis e090f3a3-5ac8-4143-bc41-ba907ae5f7c9-image.png

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

                                  VirtualVirginV 1 Reply Last reply Reply Quote 0
                                  • VirtualVirginV
                                    VirtualVirgin @d.healey
                                    last edited by

                                    @d-healey Almost...
                                    that turns it into an object, but it only returns one selection to the list instead of all of the noteCells under the selection rectangle area.
                                    Without the [0] it returns all of them, but inside an array.

                                    You can listen to my orchestral mockups here:
                                    https://www.virtualvirgin.net/

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

                                      @VirtualVirgin In that case after it returns them, just grab the [0] element from that array.

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

                                      VirtualVirginV 1 Reply Last reply Reply Quote 0
                                      • VirtualVirginV
                                        VirtualVirgin @d.healey
                                        last edited by

                                        @d-healey Whoops... is there a panic button of some sort for feedback loops? I got stuck in one.

                                        You can listen to my orchestral mockups here:
                                        https://www.virtualvirgin.net/

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

                                          @VirtualVirgin What do you mean?

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

                                          VirtualVirginV 1 Reply Last reply Reply Quote 0
                                          • VirtualVirginV
                                            VirtualVirgin @d.healey
                                            last edited by

                                            @d-healey I added a for loop after that function and it seems to be stuck in the loop

                                            You can listen to my orchestral mockups here:
                                            https://www.virtualvirgin.net/

                                            d.healeyD 1 Reply Last reply Reply Quote 0
                                            • First post
                                              Last post

                                            15

                                            Online

                                            1.8k

                                            Users

                                            11.9k

                                            Topics

                                            103.8k

                                            Posts