How to Update/Replace Specific Indices in Variable with Nested Arrays

  • feraask-1329575603262722112

    KillerK009

    23 days ago

    I have 2 variables program and reducedProgram that have identical structure and represent a workout program.

    Each has a days property which is an array of days. Each day element contains an exercises property which is an array of exercises on that day. Each exercise element contains an index property signifying it's position in that day.

    I want to take all the exercises from the reducedProgram and replace the same exercise in the program variable matching the day and exercise.index value.

    This seems like a relatively straightforward operation, but I've been trying to do this via a Toddle formula can't quite figure it out!

    I even tried a custom action as provided by the AI, but this doesn't seem to work either...
    const { program, reducedProgram } = args;

    // Iterate through the days in reducedProgram
    reducedProgram.days.forEach((reducedDay, dayIndex) => {
    // Ensure the day exists in the original program
    if (program.days[dayIndex]) {
    // Iterate through the exercises in the reduced day
    reducedDay.exercises.forEach((exercise) => {
    // Check if the exercise has an index property
    if (typeof exercise.index === 'number') {
    // Replace the exercise at the specified index
    program.days[dayIndex].exercises[exercise.index] = exercise;
    }
    });
    }
    });

    // Set the updated program to the 'program' variable
    ctx.setVariable('program', program);

    Any ideas on how to do this?
  • lucasg-1329981536744767571

    Lucas G

    22 days ago

    Can you post an example of the data
  • Or a video
  • Sounds like it could be done either way, with toddle formulas or with code
  • feraask-1329993762788610160

    KillerK009

    22 days ago

    Here's a simple workout program, it has 2 days Upper A and Upper B.

    Each day has 3 exercises with each exercise associated with a muscle group and index property which represents the order in that day.

    It's represented by this JSON data:
    {
    "days": [
    {
    "name": "Upper A",
    "exercises": [
    {
    "muscleGroup": "Chest",
    "ID": 1,
    "exercise": "Bench Press, Barbell",
    "index": 0,
    "tag": "010100",
    "label": "day01Chest00"
    },
    {
    "ID": 1,
    "muscleGroup": "Chest",
    "exercise": "Bench Press, Barbell",
    "index": 1,
    "tag": "010101",
    "label": "day01Chest01"
    },
    {
    "ID": 5,
    "muscleGroup": "Back",
    "exercise": "Pull Up, Bodyweight",
    "index": 2,
    "tag": "010500",
    "label": "day01Back00"
    }
    ],
    "dayIndex": 0
    },
    {
    "name": "Upper B",
    "exercises": [
    {
    "ID": 5,
    "muscleGroup": "Back",
    "exercise": "Pull Up, Bodyweight",
    "index": 0,
    "tag": "020500",
    "label": "day02Back00"
    },
    {
    "ID": 5,
    "muscleGroup": "Back",
    "exercise": "Pull Up, Bodyweight",
    "index": 1,
    "tag": "020501",
    "label": "day02Back01"
    },
    {
    "ID": 1,
    "muscleGroup": "Chest",
    "exercise": "Bench Press, Barbell",
    "index": 2,
    "tag": "020100",
    "label": "day02Chest00"
    }
    ],
    "dayIndex": 1
    }
    ],
    "weeks": 3,
    "units": "lb"
    }
    1329993762750992444-image.png
  • I create a copy and reduce this to a program where I only keep the last exercise for each muscle group from each day and do some calculations to add the rateGroup and numRatingExercises properties to each of these exercises:
    {
    "days": [
    {
    "name": "Upper A",
    "exercises": [
    {
    "ID": 1,
    "muscleGroup": "Chest",
    "exercise": "Bench Press, Barbell",
    "index": 1,
    "tag": "010101",
    "label": "day01Chest01",
    "numRatingExercises": 1,
    "rateGroup": "0201"
    },
    {
    "ID": 5,
    "muscleGroup": "Back",
    "exercise": "Pull Up, Bodyweight",
    "index": 2,
    "tag": "010500",
    "label": "day01Back00",
    "numRatingExercises": 2,
    "rateGroup": "0205"
    }
    ],
    "dayIndex": 0
    },
    {
    "name": "Upper B",
    "exercises": [
    {
    "ID": 5,
    "muscleGroup": "Back",
    "exercise": "Pull Up, Bodyweight",
    "index": 1,
    "tag": "020501",
    "label": "day02Back01",
    "numRatingExercises": 1,
    "rateGroup": "0105"
    },
    {
    "ID": 1,
    "muscleGroup": "Chest",
    "exercise": "Bench Press, Barbell",
    "index": 2,
    "tag": "020100",
    "label": "day02Chest00",
    "numRatingExercises": 2,
    "rateGroup": "0101"
    }
    ],
    "dayIndex": 1
    }
    ],
    "weeks": 3,
    "units": "lb"
    }


    Now I want to take all these exercises in the reduced program which have these new properties added and place each one back into the original program at the correct day and index in the given day.
  • feraask-1329995217344135198

    KillerK009

    22 days ago

    Seems like it'd be relatively straightforward with code like this:
    // Iterate through the days in reducedProgram
    reducedProgram.days.forEach((reducedDay, dayIndex) => {
    // Ensure the day exists in the original program
    if (program.days[dayIndex]) {
    // Iterate through the exercises in the reduced day
    reducedDay.exercises.forEach((exercise) => {
    // Check if the exercise has an index property
    if (typeof exercise.index === 'number') {
    // Replace the exercise at the specified index
    program.days[dayIndex].exercises[exercise.index] = exercise;
    }
    });
    }
    });


    Since I couldn't figure out how to implement this in a Toddle formula.

    But I was having issues getting my custom code to work in an action, but I guess that's because an action isn't supposed to actually update a Toddle variable directly.

    So I think I need to do it as a formula based on my findings in [my other question](https://discord.com/channels/972416966683926538/1329933922200457308/1329933922200457308)

    But it woul be nice if I could just do it as a typical Toddle formula without needing custom code I guess!
  • lucasg-1329995922352111667

    Lucas G

    22 days ago

    I’ll take a look tomorrow
    🙏1
  • lucasg-1329996272203071583

    Lucas G

    22 days ago

    I had thought about making my own simple app to track workouts a while back but ended up just using Notes lol
  • feraask-1329998533943885834

    KillerK009

    22 days ago

    Building a whole tracker may be way down the line but is probably beyond my abilities in Toddle right now 😅

    I was actually just trying to build a workout program generator that does calculations based on the muscle groups and order of days and such and outputs in a format to run it using [Liftosaur](https://www.liftosaur.com/) which is a pretty cool tracking app with a basic scripting language that allows you to add logic to your workout routine and automate things.

    I already built a decently complex [custom program](https://www.reddit.com/r/liftosaur/comments/1gt5ncf/rp_hypertrophy_program_v3_release/) in Liftosaur that automates a lot of progression and such but it takes an annoying amount of one-time manual setup to hard-code some values based on how many exercises you have per muscle group and what days they are in your program and I was hoping to use a relatively simple Toddle web app to automate that part and make it easier to just lay out exercises and then generate the output text in the Liftosaur format with all the calculations already done!
  • Tod-1329998535453577297

    Tod

    22 days ago

    Great energy @KillerK009! Your continuous contribution to the toddle Community just made you advance to Community Level 4!
  • feraask-1329999761373270146

    KillerK009

    22 days ago

    I think the output JSON should look like this:
    {
    "days": [
    {
    "name": "Upper A",
    "exercises": [
    {
    "muscleGroup": "Chest",
    "ID": 1,
    "exercise": "Bench Press, Barbell",
    "index": 0,
    "tag": "010100",
    "label": "day01Chest00"
    },
    {
    "ID": 1,
    "muscleGroup": "Chest",
    "exercise": "Bench Press, Barbell",
    "index": 1,
    "tag": "010101",
    "label": "day01Chest01",
    "numRatingExercises": 1,
    "rateGroup": "0201"
    },
    {
    "ID": 5,
    "muscleGroup": "Back",
    "exercise": "Pull Up, Bodyweight",
    "index": 2,
    "tag": "010500",
    "label": "day01Back00",
    "numRatingExercises": 2,
    "rateGroup": "0205"
    }
    ],
    "dayIndex": 0
    },
    {
    "name": "Upper B",
    "exercises": [
    {
    "ID": 5,
    "muscleGroup": "Back",
    "exercise": "Pull Up, Bodyweight",
    "index": 0,
    "tag": "020500",
    "label": "day02Back00"
    },
    {
    "ID": 5,
    "muscleGroup": "Back",
    "exercise": "Pull Up, Bodyweight",
    "index": 1,
    "tag": "020501",
    "label": "day02Back01",
    "numRatingExercises": 1,
    "rateGroup": "0105"
    },
    {
    "ID": 1,
    "muscleGroup": "Chest",
    "exercise": "Bench Press, Barbell",
    "index": 2,
    "tag": "020100",
    "label": "day02Chest00",
    "numRatingExercises": 2,
    "rateGroup": "0101"
    }
    ],
    "dayIndex": 1
    }
    ],
    "weeks": 3,
    "units": "lb"
    }


    Where it's basically the full original program but the last exercise for each muscle group in each day has been replaced with the one from the reducedProgram
  • lucasg-1330041170696474665

    Lucas G

    21 days ago

    Interesting
  • I have Strong actually lol it’s pretty good but I don’t always use it
  • Idk man creating a good routine from scratch for people sounds harder than tracking. So much can go into that
  • @KillerK009 is that right?
  • And can you verify that the output is correct
  • feraask-1330566804585578598

    KillerK009

    20 days ago

    Yup that's it!

    Take every exercise from every day in the reducedProgram object and replace the same exercise of the same day in the original program object.

    The index value on each exercise in the reducedProgram indicates what index it should replace in the exercises array in the original program.
  • lucasg-1330592921388585080

    Lucas G

    20 days ago

    1330592921006772234-image.png
  • Went off your function
  • 1330593130679894097-image.png
  • function reduceProgram({originalProgram, reducedProgram}) {
    const mergedProgram = JSON.parse(JSON.stringify(originalProgram));
    reducedProgram.days.forEach((reducedDay, dayIndex) => {
    const mergedDay = mergedProgram.days[dayIndex];
    reducedDay.exercises.forEach(reducedExercise => {
    mergedDay.exercises[reducedExercise.index] = {
    ...mergedDay.exercises[reducedExercise.index],
    ...reducedExercise
    };
    });
    });
    return mergedProgram;
    }
  • 1330593244140011602-image.png
  • feraask-1331682245865636003

    KillerK009

    17 days ago

    Thank you very much!!

    This actually inspired an idea to create a simpler custom code formula that I just call for every day inside a Map iteration like this and it seems to work!
    1331682245655658586-image.png
    1331682245320380498-image.png

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.