Skip to content

Instantly share code, notes, and snippets.

@Sylvance
Created July 28, 2016 12:26
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 Sylvance/4797ce6d35c07620608fd915e7b1d35e to your computer and use it in GitHub Desktop.
Save Sylvance/4797ce6d35c07620608fd915e7b1d35e to your computer and use it in GitHub Desktop.
https://repl.it/Cgt0/0 created by Sylvance
var removeByAttr = function(arr, attr, value){
var i = arr.length;
while(i--){
if( arr[i]
&& arr[i].hasOwnProperty(attr)
&& (arguments.length > 2 && arr[i][attr] === value ) ){
arr.splice(i,1);
}
}
return arr;
}
var arr = [{id:1,name:'serdar'},{id:2,name:'alfalfa'},{id:3,name:'joe'}];
removeByAttr(arr, 'id', 1);
// [{id:2,name:'alfalfa'},{id:3,name:'joe'}]
removeByAttr(arr, 'name', 'joe');
// [{id:2,name:'alfalfa'}]
Native Browser JavaScript
>>> => [ { id: 2, name: 'alfalfa' }, { id: 3, name: 'joe' } ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment