Are there any Toddle resources on the best method to change the index of an item in an array? Trying to implement functionality for the arrow buttons to move the current value of an object up or down. Couldn't seem to find anything here on Discord or in the docs. Thanks in advance.
There's no built in splice in toddle so if you want to use native nodes, the process is basically to find the current index and the new drop index and use those values to split the array and dropping the item in there
π1
There's a few steps involved so I prefer simply using a custom formula
Lucas G
2 months ago
Oct 11, 2024, 3:15 PM
Here's an example of what the formula could look like:
Lucas G
2 months ago
Oct 11, 2024, 3:16 PM
function moveArrayItem(args) { if (args.fromIndex < 0 || args.fromIndex >= args.array.length || args.toIndex < 0 || args.toIndex >= args.array.length) { throw new Error ("Index out of bounds"); } const newArray = [...args.array]; const [item] = newArray.splice(args.fromIndex, 1); newArray.splice(args.toIndex, 0, item);
return newArray; }
Tom Ireland
2 months ago
Oct 11, 2024, 3:18 PM
Andreas did an awesome video on drag and drop reordering that would probably apply here. What do you think, Lucas? I implemented this myself with formulas following Andreasβ demo. I can dig it out if itβs applicable.
Yeah, itβs good. I think adding splice as a formula would save a bunch of time though and probably reduce the need for having a custom action as well?
Lucas G
2 months ago
Oct 11, 2024, 3:38 PM
Yes, it would. I don't recall if there is a particular reason why splice is not in the list
I think it's been suggested but I don't recall what was said about it
Thanks for taking the time to reply - I'll get this implemented! Sounds like a solid approach. I do wish something this relatively basic was supported with Toddle's internal tools. It's not as if an array splice is uncommon requirement.
Max
1 month ago
Oct 14, 2024, 1:20 PM
If this is just for reordering, you don't need splice. You can just do a map to create new indexes and then a sort. That's at least how I do it