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?

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].

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.