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
HardlyNoticeable
5171 gold badge9 silver badges27 bronze badges
-
11 and 2 probably point to the same object.jlowcs– jlowcs2015年03月17日 21:58:05 +00:00Commented Mar 17, 2015 at 21:58
1 Answer 1
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
JuniorCompressor
20.1k4 gold badges33 silver badges58 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-js