Logo

Programming-Idioms

History of Idiom 135 > diff from v47 to v48

Edit summary for version 48 by sanjarcode:
[JS] Comment was long

Version 47

2021-03-08, 08:26:58

Version 48

2021-03-08, 08:28:56

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)
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.
Comments bubble
// OK if x is absent, no errors.
Doc URL
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
Doc URL
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/splice
Demo URL
https://gist.github.com/sanjarcode/2447f2f59fbcc4428b5b4269ddb46182
Demo URL
https://gist.github.com/sanjarcode/2447f2f59fbcc4428b5b4269ddb46182