Created
June 15, 2022 03:24
-
-
Save DaveyJake/f6c98085169dabf443a6022192ac26de to your computer and use it in GitHub Desktop.
[JS] Missing Array Method: Delete
This file contains hidden or 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
/** | |
* This method allows developers to easily remove any element | |
* from any array. | |
* | |
* @author Davey Jacobson <daveyjake21 [at] geemail [dot] com> | |
* | |
* @param {mixed} element Any specified element in any array. | |
* | |
* @return {array} This array instance without the specified | |
* element if successful. The original array | |
* if not successful. | |
*/ | |
Array.prototype.delete = function( element ) { | |
const index = this.indexOf( element ); | |
if ( index > -1 ) { | |
this.splice( index, 1 ); | |
} | |
return this; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment