Logo

Programming-Idioms

History of Idiom 135 > diff from v39 to v40

Edit summary for version 40 by programming-idioms.org:
Admin deletes impl 2876: This doesn't properly handle x not found and indexOf returning -1

Version 39

2020-09-30, 09:10:42

Version 40

2020-09-30, 09:12:12

Idiom #135 Remove item from list, by its value

Remove at most 1 item from list items, having value x.
This will alter the original list or return a new list, depending on which is more idiomatic. If there are several occurrences of x in items, remove only one of them.

Idiom #135 Remove item from list, by its value

Remove at most 1 item from list items, having value x.
This will alter the original list or return a new list, depending on which is more idiomatic. If there are several occurrences of x in items, remove only one of them.

Variables
items,x
Variables
items,x
Code
const firstIndex = _items.indexOf(x)
const newList = _items.splice(firstIndex, 1)
Comments bubble
Removes the first instance of x from the array, leaving others intact