In the two objects below, I want to compare their equality by testing all their attributes (name, modifiers, price) EXCEPT for one attribute (quantity).
As in the example below, if I simply compared the two objects (Obj A
& Obj B
) they would be equal in every case, except for their quantity attribute. I'm simply looking to exclude quantity when doing a comparison between the two objects.
Obj A
{
name: "Bud Lite",
price: 399,
modifiers: null,
quantity: 2,
}
Obj B
{
name: "Bud Lite",
price: 399,
modifiers: null,
quantity: 1,
}
Here's how I'm currently doing it:
if (JSON.stringify(ObjA) === JSON.stringify(ObjB)) {
return true;
}
You can use spread (...)
before to exclude the quantity or any other properties
const { quantity: quantitya, ...a } = {
name: "Bud Lite",
price: 399,
modifiers: null,
quantity: 2,
}
const { quantity: quantityb, ...b } = {
name: "Bud Lite",
price: 399,
modifiers: null,
quantity: 1,
}
console.log(JSON.stringify(a) === JSON.stringify(b))