List.remove
remove($index, $numItems=1) -> any
Description
Remove an item starting at $index
and return its value.
If $index
is negative, it counts from the end of the list (the last item is at -1
).
If an item does not exist at that index, it returns the default value of the list (e.g. an empty string). If $numItems
is greater than one, it returns an empty list.
This function is the opposite of $insert
.
$items = ['a', 'b', 'c', 'd', 'e'] $items.remove(1) //= 'a' print($items) //= ['b', 'c', 'd', 'e'] $items.remove(-2) //= 'd' print($items) //= ['b', 'c', 'e'] $items.remove(99) //= ''
Multiple Items
If numItems
is more than 1, the values will be returned as a List.
If items do not exist at the index, an empty list will be returned.
$items = ['a', 'b', 'c', 'd', 'e'] // Remove the first 2 items $items.remove(1, 2) //= ['a', 'b'] print($items) //= ['c', 'd', 'e'] $items.remove(99, 2) //= []
Splicing
To do the equivalent of a “splice”, follow up with a call to insertAll
at the same index.
$items = ['a', 'b', 'c', 'd', 'e'] $items.remove(3, 2) //= ['a', 'b', 'e'] $items.insertAll(['X', 'Y', Z'], 3) //= ['a', 'b', 'X', 'Y', 'Z', 'e']