I am trying to swap two object values in JavaScript using the [] = [] method, but my below code fails with an error saying "message": "Uncaught TypeError: Cannot set property '9' of undefined",
let dataObj={"reg_price":2, "reg_price_alt":5, "ex":9}
console.log("before: ", dataObj)
[dataObj.reg_price, dataObj.ex] = [4, 5];
console.log("after: ", dataObj)
Is there some syntax I'm missing? I don't understand why this simple code does not work.
[dataObj.reg_price, dataObj.ex] = [dataObj.ex, dataObj.reg_price];which works with variables not in an object({ dataObj.reg_price, dataObj.ex } = { dataObj.reg_price: 10, dataObj.ex: 20 });but it doesnt work