I have:
var array = new Array(); array.push("A"); array.push("B"); array.push("C");
I want to be able to do something like:
array.remove("B");
but there is no remove function. How do I accomplish this?
Advertisement
Answer
I’m actually updating this thread with a more recent 1-line solution:
let arr = ['A', 'B', 'C']; arr = arr.filter(e => e !== 'B'); // will return ['A', 'C']
The idea is basically to filter the array by selecting all elements different to the element you want to remove.
Note: will remove all occurrences.
EDIT:
If you want to remove only the first occurence:
t = ['A', 'B', 'C', 'B']; t.splice(t.indexOf('B'), 1); // will return ['B'] and t is now equal to ['A', 'C', 'B']