myObj = {
prop1: 'alpha',
prop2: 'beta',
priceUpdatedOn: new Date()
};
myObjQuery = myObj;
delete myObjQuery.priceUpdatedOn;
console.log(myObj);
console.log(myObjQuery);
When I do that, the priceUpdatedOn gets deleted from myObj as well for some reason. Any idea why?
Javascript works with references. myObjQuery and myObj are references to the same data in memory. Altering property of one will alter the copy in memory, and hence – all references to it. You have to clone the object instead