Remove object from array

Simple noob question… what is the equivalent of remove object at index that I’ve used in swift. I can’t seem to do anything other than spawn more arrays without the object (using MAP and WITHOUT). It’s driving me mad.

So if you have e.g. pageVars.myArray with objects like

[{id: 0, fruit: "banana"}, {id: 1, fruit: "orange"}, {id: 2, fruit: "apple"}]

to remove the object with ID 1, you’d simply use Set page variable to set myArray to

REMOVE_ITEM_BY_KEY(pageVars.myArray, "id", 1)

There’s no way to manipulate the array “in place” by reaching into it and removing a single item; you always replace the whole array.

This can cause unwanted behaviour with e.g. long lists as all components get remounted; with our upcoming new runtime, this will be smarter and only the truly changed components are remounted.

1 Like

That’s exactly the issue I have - because if i use REMOVE_ITEM_BY_KEY in a MAP formula - I get zillions of nested arrays each with the item removed!!!

You can’t use MAP to remove items – by its nature, the output of MAP always contains exactly as many items as the input, as the function that determines how the item is transformed is run once for every item; there’s no option of “instead of transforming this object, throw it away”.

So you need to do either MAP or REMOVE_ITEM_BY_KEY first, then use that output as the input array for the other formula.

Or alternatively, use the SELECT formula which returns just the items that match the criteria.

I posted an example here Iterating though an array

Cheers - i’ll have a play today!