0

I have created below function to delete a custom element from an array:

 Array.prototype.removeElement=function(x){
    var index = this.indexOf(x);
    if (index !== -1) {
      this.splice(index, 1);
    }
};

It works fine with below array:

var data = [1,2,3,4];
data.removeElement(2); //returns [1,3,4]

But when I have more than one item from a certain element it removes only first occurrence.

var data = [1,2,3,4,2];
data.removeElement(2);
// returns [1,3,4,2] while I expect to get [1,3,4]

I know I can do this by using loops, But I am curious to know if there is any cleaner code?

Behnam
  • 1,039
  • 2
  • 14
  • 39
  • 1
    Does this answer your question? [JavaScript remove all occurrences of a value from an array](https://stackoverflow.com/questions/48608119/javascript-remove-all-occurrences-of-a-value-from-an-array) – kmoser Aug 21 '21 at 05:42
  • There isn't really a way to delete multiple elements in place without a loop, unless you do something like [this](https://stackoverflow.com/a/49587869/5648954), but that's a little hacky (and technically still using a _loop_) and isn't any better than using a standard loop, so your better off just using something like [a for/while loop](https://stackoverflow.com/a/9425230/5648954) – Nick Parsons Aug 21 '21 at 05:49

3 Answers3

1

2 solutions: one returns a new array and the other does it in-place

Solution 1: returns a new array

You can leverage the built-in filter method

function removeAllOccurences (array, element) {
  return array.filter((ele) => ele !== element);
}

console.log(removeAllOccurences([1,2,3,4,3],3)); // [1,2,4]

Solution 2: in-place using recursion

function removeAllOccurences (array, element) {
  if (!array.includes(element)) {
    return array;
  } else {
    let index = array.indexOf(element);
    array.splice(index, 1);
    return removeAllOccurences(array, element);
  }
}

console.log(removeAllOccurences([1,2,3,4,3],3)); // [1,2,4]
jgrewal
  • 189
  • 1
  • 2
  • 11
1

Using JS .filter() array method can be handy. Try this code,


// Create a function in array prototype as 
Array.prototype.removeElement = function(x){
    return this.filter((elem)=>elem!==x);
}

This should work a charm, but I don't think. There is any other way to do this other than looping.

enter image description here

Nirjal Paudel
  • 201
  • 5
  • 11
0

Try a while loop to continue using the splice method until that element is no longer present.

 Array.prototype.removeElement=function(x){
    var index = this.indexOf(x);
    if (index !== -1) {
      while (this.includes(x)) {
      index = this.indexOf(x);  
      this.splice(index, 1);
      }
    }

}
   

The while loop uses the array.includes method to determine whether the array still contains that element's value, and if it does, it updates the index to the next element x, after which it will then splice the element like your code did. The while loop breaks when array.includesis false in turn, removing all the elements equal to x from the array.

Russ
  • 36
  • 1
  • 4