Assuming "equal" means "having the same id", you just need a loop and an if test:
function removeItem(array, item) {
for (var i = array.length - 1; i >= 0; i--)
if (array[i].id === item.id) {
array.splice(i, 1);
break; // remove this line if there could be multiple matching elements
}
}
var array = [
{ id: 1, color: "red"},
{ id: 2, color: "blue"},
];
removeItem(array, { id: 2, color : "blue"});
Obviously you can extend this to test both properties:
if (array[i].id === item.id && array[i].color === item.color) {
If you wanted to be able to compare arbitrary objects with unknown property names then instead of a simple if
test you could use a nested for..in
loop to test each property, or perhaps something like this (untested):
function equalObjects(o1, o2) {
var okeys1 = Object.keys(o1);
if (okeys1.length != Object.keys(o2).length)
return false;
for (var i = 0; i < okeys1.length; i++)
if (o1[okeys1[i]] !== o2[okeys1[i]])
return false;
return true;
}
and then in my original function:
if (equalObjects(array[i], item)) {