2

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.
}
2
  • 1
    I guess $this is a variable referring to some jquery object. DId u check that $this.parents() selects the right elements? Commented Apr 30, 2022 at 16:34
  • Thanks @cheesyMan. Yes, I checked. Thanks. Commented Apr 30, 2022 at 16:37

1 Answer 1

4

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();

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.