Created
July 28, 2016 12:26
-
-
Save Sylvance/4797ce6d35c07620608fd915e7b1d35e to your computer and use it in GitHub Desktop.
https://repl.it/Cgt0/0 created by Sylvance
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
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'}] |
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
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