Logo

Programming-Idioms

History of Idiom 135 > diff from v22 to v23

Edit summary for version 23 by joewrong:
[JS] read the instructions after the implementation lol

Version 22

2019-09-26, 16:46:05

Version 23

2019-09-26, 16:56:17

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.

Code
function removeItemFromList(list, item) {
  const foundIndex = list.find(li => {
    return li === item;
  });
  list.splice(foundIndex, 1);
  return list;
}
Code
const index = items.find(li => li === x);
items.splice(index, 1);