Skip to content

Instantly share code, notes, and snippets.

@DaveyJake
Created June 15, 2022 03:24
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 DaveyJake/f6c98085169dabf443a6022192ac26de to your computer and use it in GitHub Desktop.
Save DaveyJake/f6c98085169dabf443a6022192ac26de to your computer and use it in GitHub Desktop.
[JS] Missing Array Method: Delete
/**
* 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