gpt4 book ai didi

javascript - Javascript 中奇怪的数组赋值

转载 作者:行者123 更新时间:2023-11-29 18:09:04 25 4
gpt4 key购买 nike

我有以下内容:

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}}}

为什么 scenario_data[1]['amount']['value']scenario_data[2]['amount']['value'] 是设置为 1234?

最佳答案

因为scenario_data[0]scenario_data[1]指向同一个对象。因此,当您转换为 json 时,两个元素具有相同的表示形式。您可以通过检查 scenario_data[0] == scenario_data[1] 的值来验证。

关于javascript - Javascript 中奇怪的数组赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29110432/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com