I am building out a shopping cart component and have a function to add new items. My object structure looks like so:
productsArray = [{
"item": {
"id": 001,
"name": "dinner plate",
"description": "white corningware plate",
"collection": "dinner",
"price": 50,
},
"qty": 1,
}]
if my addProduct function is called on a different item, it pushes a new object to the array. However I want to ensure that if some code runs in the application such as delete, pop or an attempt to change a property's value - it will not change the object. I still, however, would like to be able to change the qty
value if the addProduct function executes and there is a matching object (which will execute qty += 1
).
I understand there are Object.freeze and Object.seal at my disposal but I am running into errors in allowing only the one property to be modified. Any help would be greatly appreciated.