PricingDocsAcademy
Bluesky ...
Wed, Dec 11, 9:42 PM

Change Index of Item in an Array

  • /attachments/1294303453639610428/1294303453803184229/Screenshot_2024-10-11_at_10.17.03_AM.png

    Matthewcc

    2 months ago

    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.
  • Lucas G

    2 months ago

    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
  • /attachments/1294303453639610428/1294317492264632431/image.png

    Lucas G

    2 months ago

    Here's an example of what the formula could look like:
  • Lucas G

    2 months ago

    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

    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.
  • Lucas G

    2 months ago

    Yup I've done it that way as well
  • But changed to the custom formula way
  • Tom Ireland

    2 months ago

    Righto. Fair play. I found it. See https://discord.com/channels/972416966683926538/1096473847651389549

    Check out Andreas’ Loom link in there.
  • Lucas G

    2 months ago

    Nice, that's a good video
  • It's how I started with my drag and drop list
  • Tom Ireland

    2 months ago

    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

    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
  • Matthewcc

    1 month ago

    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

    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

Stop scrolling. Start building.

toddle is a visual web app builder that rivals custom code β€” but accessible to your entire team!

Try toddle β€” it's free!

Β© Copyright 2024 toddle. All rights reserved.