If you want to remove all the elements of the array with a certain ID you can do this:
function removerID(id, arr) {
return arr.map(function (obj) {
if (obj.id != id) return obj;
else return false;
}).filter(Boolean);
}
jsFiddle: link
In this case I suggest you do not change the initial array. You can always do arr = removerID(1, arr);
, but if you want to change the array even without passing it to the function you can do the assignment inside the function:
function removerID(id) {
arr = arr.map(function (obj) {
if (obj.id != id) return obj;
else return false;
}).filter(Boolean);
}
Another alternative is to make a map with indexes of objects that have a certain ID but this only makes the code more complex and I doubt it will greatly improve the performance of qq would be something like this:
function removerID(id) {
arr.map(function (obj, i) {
if (obj.id == id) return i;
else return 'foo';
}).reverse().map(function (i) { // uso o reverse para ele usar indexes decrescentes
if (typeof i == 'number') arr.splice(i, 1);
})
}
removerID(1);
jsFiddle: link