Array remove element

by Kumar Sidharth

HTML

<label>remove</label> <output class="remove"></output>
<label>removeAll</label> <output class="removeAll"></output>
<br>
The element at first index is removed
<hr>
For remove all, filter can be used
<!-- <hr>
<label for="">removeAll</label>
<output class="removeAll"></output> -->

CSS

body {
  background: black;
  color: #999;
}

TypeScript

const arr: number[] = [1, 2, 3, 2, 2, 4, 5, 6, 2];

Array.prototype.remove = function(el: T) {
  const index = this.findIndex(e => { return e === el });
  if (index > -1) {
    this.splice(index, 1);
  }
}

arr.remove(2);
document.querySelector('output.remove').innerText = (JSON.stringify(arr)); 


/* Array.prototype.removeWithApply = (el: T) => {
  const index = this.filter(e => { return e === el });
  if (index > -1) {
    this.splice(index, 1);
  }
}

arr.removeWithApply.apply(arr, [2]);
console.log(arr); */