How to keep original properties in an array and just add new ones to each element?

  • /attachments/1325975941423435868/1325975941830414336/image.png

    KillerK009

    8 days ago

    Is there an easy way or shorthand in a Toddle formula to take every object in an array and just add an additional value/property to each one while retaining all the existing values? I asked the Toddle AI and it mentioned using the Map function and ...item to spread the properties but I don't understand what this means exactly or how to do so in a Formula.

    Does this just mean mapping each item to a new object and having to re-define the key/value pairs for every property again like I did in the attached screenshot? This isn't a huge deal on a smaller object with only a couple properties per element, but when there are many values that all need to be mapped to the same thing again it feels very tedious.

    For example, I have an array of exercises in my workout program and I want to go through every exercise in the array and add a tag property (where there's some formula to calculate the tag value).

    So if my exercise array is like:
    [
    {
    "muscleGroup": "Chest",
    "ID": 1,
    "exercise": "Bench Press, Barbell"
    },
    {
    "muscleGroup": "Front & Side Delts",
    "ID": 2,
    "exercise": "Lateral Raise, Dumbbell"
    },
    {
    "muscleGroup": "Chest",
    "ID": 1,
    "exercise": "Bench Press, Dumbbell"
    }
    ]


    I'd like the output to be:
    [
    {
    "muscleGroup": "Chest",
    "ID": 1,
    "exercise": "Bench Press, Barbell",
    "tag": 0
    },
    {
    "muscleGroup": "Front & Side Delts",
    "ID": 2,
    "exercise": "Lateral Raise, Dumbbell",
    "tag": 0
    },
    {
    "muscleGroup": "Chest",
    "ID": 1,
    "exercise": "Bench Press, Dumbbell",
    "tag": 1
    }
    ]


    So I keep all the original properties and values and just add a tag to each element.
    image.png
  • /attachments/1325975941423435868/1325999259446804530/image.png

    KillerK009

    8 days ago

    Think I figured it out. In case anyone else was wondering I was able to do this using the Set function to basically just add the extra property to each item.

    Not sure if this is what the ...item thing about spreading the properties meant exactly but I guess it works!
    image.png
  • Lucas G

    8 days ago

    Glad you figured it out. The spread thing is a concept in coding but you figured out the toddle way which is what you needed 👍
  • Andreas Møller

    8 days ago

    👍
  • You can use concatenate to merge the properties of two objects
    👍1
  • KillerK009

    7 days ago

    Can you show an example of how to do this? Like if I want to add 2 properties to every element in an array I can use Map with Concatenate instead of 2 Set functions?

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.