Skip to content

Instantly share code, notes, and snippets.

@smling
Last active November 26, 2021 07:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save smling/8dfe53aa51374adedb82566c304d1301 to your computer and use it in GitHub Desktop.
Save smling/8dfe53aa51374adedb82566c304d1301 to your computer and use it in GitHub Desktop.
javascript-array-remove
/**
* Remove array element(s) by value.
* @param {Value to be remove} value
* @returns Array which removed value.
*/
Array.prototype.remove = function(value) {
let index = 0;
do {
index = this.indexOf(value);
if(index >=0)
this.splice(index, 1);
} while(index < 0)
}
// Test
let months = ["Jan", "Feb","April","April", "March", "April", "May"];
console.log("Before run remove(): " + months); // Output: "Before run remove(): Jan,Feb,April,April,March,April,May"
months.remove('April');
console.log("After run remove(): " + months); // Output: "After run remove(): Jan,Feb,March,May"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment