Adding an element to an array: Push and Append replacing the array, rather than adding on to it

Options
Hi! I've created a Function with a For Loop, where at the end of the loop, a new variable is created, and I want to add this variable to an array. I've tried both Push and Append. However, with each one, the entire array is replaced with the new variable as an element, rather than the new variable added on to the end of the array. Here's a video showing my set up and the results: https://www.loom.com/share/7dbd4aa6f2864916a809b30eee48946c

I didn't show it in the video, but I also tried Merge, which ended up with the same result as Push and Append shown above. Thanks for your help!

Comments

  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
    I would use the Array Function, Array: Add to End of Array (or also Add to Beginning of Array - depending on preference)
  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
    (Function Stack > Data Manipulation > Arrays)
  • Marianna Kerppola
    Options
    Thanks ! I tried both Add to End and Add to Beginning of Array, but ended up with the same results as last time, where rather than adding the new item on, it replaced it altogether. Here's a Loom video showing the results with Add to End of Array: https://www.loom.com/share/6eacfb05bb144a0e918398d4f0501bfd

    Anything else you would recommend? Thanks for your advice! 
  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
    Okay I'm pretty sure I've identified the issue... and it gets confusing with all the conditional statements and nested functions. But the array you are trying to update is actually being established inside the For Each Loop which is why you are only getting the final iteration. You will need to establish that array of user.trimester outside of the array and then you can add each iteration of the new value to that array... I see there are times that this array is established outside but due to the conditional logic, it seems when it's running through this the array is being established inside the for each loop - not outside.

    Let me know if that makes sense or not
  • Marianna Kerppola
    Options
    Makes perfect sense ! I created separate array outside of the loop, and it worked as expected. Thanks for your help!
  • Xanoguy
    Xanoguy Member
    Options

    I'm having the same result. I tried with push and append, and confirmed my creation of the array is outside of the loop, but the array is simply replaced by the new value rather than appended.

  • Xanoguy
    Xanoguy Member
    Options

    Based on Michael's first suggestion, I created a new function, starting with the function stack "Array Function, Array: Add to End of Array (or also Add to Beginning of Array ". This worked as expected, but based on my testing, starting with "Update Variable" and then using the "append" or "push" function within "update variable" function, simply replaced the array with the new value. May be something to address if you can reproduce. This post was helpful in highlighting I should try another way, so gracias.