Last active
November 26, 2021 07:04
-
-
Save smling/8dfe53aa51374adedb82566c304d1301 to your computer and use it in GitHub Desktop.
javascript-array-remove
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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