2

I've got the following:

alert("before: " + JSON.stringify(scenario_data)); // Outputs: {"1":{"amount":{"value":"","inputflag":false},"numberout":{"value":"","inputflag":false},"discount":{"value":"","inputflag":false}},"2":{"amount":{"value":"","inputflag":false},"numberout":{"value":"","inputflag":false},"discount":{"value":"","inputflag":false}}}
scenario_data[1]['amount']['value'] = 1234;
alert("After: " + JSON.stringify(scenario_data)); // Outputs: {"1":{"amount":{"value":1234,"inputflag":true},"numberout":{"value":"","inputflag":false},"discount":{"value":"","inputflag":false}},"2":{"amount":{"value":1234,"inputflag":true},"numberout":{"value":"","inputflag":false},"discount":{"value":"","inputflag":false}}}

Why are both scenario_data[1]['amount']['value'] and scenario_data[2]['amount']['value'] being set to 1234?

Aswin Ramakrishnan
3,2132 gold badges43 silver badges65 bronze badges
asked Mar 17, 2015 at 21:54
1
  • 1
    1 and 2 probably point to the same object. Commented Mar 17, 2015 at 21:58

1 Answer 1

4

Because scenario_data[0] and scenario_data[1] point to the same object. So, when you convert to json, both elements have the same representation. You could verify by checking the value of scenario_data[0] == scenario_data[1].

answered Mar 17, 2015 at 21:55
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.