How to Delete an Item from a LIST

To delete a ROW from a TABLE we use the DeleteRows() formula.

But there is not a similar formula for deleting an ITEM from a LIST and it is not obvious how to do this with other formulas.

If you know the position, N, of your item in the list you can use…

myList.SetControlValue( myList.Splice( N,1) )

The Splice() formula takes 2 parameters;

  • the start position in the list to operate on - we use N
  • the number of items to delete at that position - we use 1
  • additional parameters can be added; they are the items to be inserted - we ignore that here

But if you do not know the position of the ITEM you want to delete you can use…

myList.SetControlValue( myList.Filter( CurrentValue != ITEM ) )

The Filter() applies the parameter (as a conditional expression) to each item in the list.
This will return a new list containing only elements that evaluated as TRUE.
The condition CurrentValue != ITEM will evaluate as…

  • TRUE for all elements that are not ITEM
  • FALSE for any elements that are equal to ITEM

So the result is to return a list with all the original elements EXCEPT any that are equal to ITEM.

Note that a good precaution is to use the ListCombine() formula.
I have seen a great many cases where the list is not flat, but contains “Lists within Lists”.
Adding the ListCombine() will ensure that you get the expected behavior in all cases.

So the formula becomes…

myList.SetControlValue( ListCombine(myList) .Filter( CurrentValue != ITEM) )

respect
Max

11 Likes

Somebody asked how to delete a set of ITEMS from a LIST

The easiest way I can think of is…

myList.SetControlValue(
    myList.filter( CurrentValue.In( ITEMS ).not() )
)

ie: return all the elements in myList that are NOT in ITEMS.

I also had to point out that these tricks do not need to be in a SetControlValue(..)
to work, they might just as easily be used with thisRow.modifyRows(...) as well.

Max

1 Like