How do I find and remove objects in an array?
-
@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.
-
@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.
-
@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.
-
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.
-
@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. -
@VirtualVirgin For those functions (some, filter, etc.) the first parameter should be a function.
Edit: I just realised you're doing the javascript arrow function thing, we don't have that in HISE.
-
@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.
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:
-
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?
-
@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. -
@VirtualVirgin Add
[0]
after the parenthesis -
@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. -
@VirtualVirgin In that case after it returns them, just grab the [0] element from that array.
-
@d-healey Whoops... is there a panic button of some sort for feedback loops? I got stuck in one.
-
@VirtualVirgin What do you mean?
-
@d-healey I added a for loop after that function and it seems to be stuck in the loop
-
@VirtualVirgin What does the loop do?
-
for (i = 0; i < noteCells; i++) selectedNotes.push(noteCells[i]);
It's not resolving because I meant to use "i < noteCells.length",
so I am stuck with a spinning wheel and a sluggish HISE.I'll just force quit it.
Just didn't know if there is a button similar to "all notes off" to stop the processing. -
@VirtualVirgin Can you just hit compile again?