Change Index of Item in an Array

  • matthewcc119-1294303453639610428

    Matthewcc

    5 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.
    1294303453803184229-
  • lucasg-1294316687700529182

    Lucas G

    5 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
  • lucasg-1294317492679737465

    Lucas G

    5 months ago

    Here's an example of what the formula could look like:
    1294317492264632431-
  • lucasg-1294317803821862976

    Lucas G

    5 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;
    }
  • tomthebigtree-1294318112048550008

    Tom Ireland

    5 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.
  • lucasg-1294318212384555040

    Lucas G

    5 months ago

    Yup I've done it that way as well
  • But changed to the custom formula way
  • tomthebigtree-1294318764212617287

    Tom Ireland

    5 months ago

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

    Check out Andreas’ Loom link in there.
  • lucasg-1294319077694902457

    Lucas G

    5 months ago

    Nice, that's a good video
  • It's how I started with my drag and drop list
  • tomthebigtree-1294321661012738110

    Tom Ireland

    5 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?
  • lucasg-1294323292752318577

    Lucas G

    5 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
  • matthewcc119-1295360053859319818

    Matthewcc

    5 months 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.kayr-1295375711732895804

    Max

    5 months 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.