'Add property & value from variable to existing object in JavaScript
I am trying to add properties & values from variable to existing object in JavaScript. My code is like below.
var partno = $this.parents('.cart_item').data('partno');
if (localStorage.getItem("shipping_values")) {
var shipping_values = localStorage.getItem("shipping_values");
shipping_values[partno] = $this.val();
console.log(shipping_values); // I am not getting updated values here.
}
Solution 1:[1]
Values received from localStorage are in strings. You should run JSON.parse and then add values to object.
var shipping_values = JSON.parse(localStorage.getItem("shipping_values"));
shipping_values[partno] = $this.val();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Inder |
