Logo

Programming-Idioms

History of Idiom 135 > diff from v46 to v47

Edit summary for version 47 by sanjarcode:
New JS implementation by user [sanjarcode]

Version 46

2021-01-07, 09:29:31

Version 47

2021-03-08, 08:26:58

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. If x is absent, keep items unchanged.

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. If x is absent, keep items unchanged.

Variables
items,x
Variables
items,x
Code
items.splice(items.indexOf(x), 1)
Comments bubble
// Deltes first occurrence of x
// Successive elements moved to the left by 1 step.
// Edge case - If the x is absent, splice doesn't throw an error. So it's safe.
Doc URL
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
Demo URL
https://gist.github.com/sanjarcode/2447f2f59fbcc4428b5b4269ddb46182