Skip to content

Instantly share code, notes, and snippets.

@sergeliatko
Last active August 23, 2017 18:00
Show Gist options
  • Save sergeliatko/51a83ae72b00787f9efd856683f98b32 to your computer and use it in GitHub Desktop.
Save sergeliatko/51a83ae72b00787f9efd856683f98b32 to your computer and use it in GitHub Desktop.
Removes value from an array
function removeValue( v, a ) {
if( a.length ) {
var i = a.indexOf(v);
if( -1 === i ) {
return a;
}
a.splice( i, 1 );
return removeValue( v, a );
}
return a;
}
@sergeliatko
Copy link
Author

sergeliatko commented Aug 22, 2017

Or it can be done as an extension to an array prototype like this:

Array.prototype.removeByValue = function(value) {
    var i = this.indexOf(value);
    if( -1 === i ) {
        return this;
    }
    this.splice( i, 1 );
    return this.removeByValue(value);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment