Add an object to object within pageVars

I try to make a workout page where I can add multiple exercises to a workout. For now I can log a workout with multiple exercises. But how do i add sets (objects) to an idividual exercise?

So this is my pageVars structure:
editWorkouts

  • editing
  • id
  • name
  • power
  • settings
  • weight

I can add a exercise with:
INSERT_ITEM_AT(pageVars.editWorkouts, 1,{name: repeated.current.name, weight: repeated.current.weight, settings: repeated.current.settings, power: repeated.current.power})

After editing an excercise I store the objects with: SET_ITEM_AT(pageVars.editWorkouts, INDEX_OF(pageVars.editWorkouts, repeated.current), {name: repeated.current.name, weight: repeated.current.weight, settings: repeated.current.settings, id: repeated.current.id , power: repeated.current.power, editing: false})

After that I can store the pageVars to my local database. So far it works.

Now I want to add the following to the exercises:

  • sets (list of objects)
    • id
    • reps
    • setnumber
    • weight

So it becomes:

editWorkouts

  • editing
  • id
  • name
  • power
  • settings
  • weight
  • sets (list of objects)
    • id
    • reps
    • setnumber
    • weight

I think it must be something like this but that messes up everything:

INSERT_ITEM_AT(pageVars.editWorkouts, INDEX_OF(sets, -1),INSERT_ITEM_AT(sets, 1,{reps: “1”, weight: “1”, setnumber: “1”}))

Hi @Lucas_Bonsel, you could use the MAP formula for this. It maps over all the items in the list and transforms them to something else. The following formula will apply logic to items that match the IF condition, and just return the workout as it is if the id doesn’t match:

MAP<workout>(pageVars.editWorkouts, IF(workout.id === repeated.current.id, <add set>, workout))

Then what you want to use in place of < add set > is a SET_KEY and WITH_ITEM combo (it automatically adds to the end of the list so you don’t need to use INSERT_ITEM_AT:

SET_KEY(workout, "sets", WITH_ITEM(workout.sets, {reps: “1”, weight: “1”, setnumber: “1”}))

Let me know if it worked for you!

I think it will work but i think i am missing a step:
First i want to be able to add different sets. I have a form with several input fields for name, power, etc.

Now i want to add a repeating component for sets. Like this:

After adding xx sets I press the last button to “save all” into the pagevar.

You can do a nested repeat with repeated.current.sets list from the first level current repeat.

You need to give the second repeat a name in the “repeat as” field, such as sets, so you don’t have two repeats with the same name current. Then you can repeat the set fields with repeated.sets.reps.

1 Like