Created
April 21, 2014 18:31
-
-
Save danman01/11151784 to your computer and use it in GitHub Desktop.
remove single item from array in javascript
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
/* Removes first specified item from array, if it exists | |
Use like: ["55","35"].pull("35") | |
Handles cases where "35" isn't in the array | |
Modifies array and returns pulled item | |
*/ | |
Array.prototype.pull = function(x) { | |
var index = this.indexOf(x); | |
var to_return = undefined; | |
if(index !== -1) { | |
this.splice(index, 1); | |
to_return = x; | |
} | |
return to_return; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment