1

I am having problems trying to sort this on my own.

I have a hidden field containing a small amount of json.

I populate a variable using

 $(document).ready(function() {
var data = $("#result").text();
var j = JSON.parse(data);
j.my_item.total_price==="2222";
console.log(j.my_item.total_price);

});

the variable j is showing the correct data, I just don't have a clue how to update the total_price

Can anyone suggest what I need to do to enable me to update total_price?

1
  • 2
    Did you try j.my_item.total_price="2222"; ? Commented Jan 25, 2012 at 6:23

2 Answers 2

9

You can assign to an object property just like any other variable:

j.my_item.total_price = "2222";

Or the alternative (array-like) syntax:

j['my_item']['total_price'] = "2222";

Or mix-and-match:

j.my_item['total_price'] = "2222";
j['my_item'].total_price = "2222";
Sign up to request clarification or add additional context in comments.

Comments

3
$(document).ready(function() {
var data = $("#result").text();
var j = JSON.parse(data);
j.my_item.total_price="2222";
console.log(j.my_item.total_price);

});

=== isn't an assignment operator, it's a type-strict comparison operator.

See here:

Difference between == and === in JavaScript

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.