• <legend id='eV0UN'><style id='eV0UN'><dir id='eV0UN'><q id='eV0UN'></q></dir></style></legend>
    <tfoot id='eV0UN'></tfoot>
    1. <small id='eV0UN'></small><noframes id='eV0UN'>

        <i id='eV0UN'><tr id='eV0UN'><dt id='eV0UN'><q id='eV0UN'><span id='eV0UN'><b id='eV0UN'><form id='eV0UN'><ins id='eV0UN'></ins><ul id='eV0UN'></ul><sub id='eV0UN'></sub></form><legend id='eV0UN'></legend><bdo id='eV0UN'><pre id='eV0UN'><center id='eV0UN'></center></pre></bdo></b><th id='eV0UN'></th></span></q></dt></tr></i><div id='eV0UN'><tfoot id='eV0UN'></tfoot><dl id='eV0UN'><fieldset id='eV0UN'></fieldset></dl></div>
          <bdo id='eV0UN'></bdo><ul id='eV0UN'></ul>

        为什么更新一个对象的属性会改变另一个对象?

        Why does updating properties in one object change another object?(为什么更新一个对象的属性会改变另一个对象?)
          <tbody id='567sA'></tbody>

            <bdo id='567sA'></bdo><ul id='567sA'></ul>
            <legend id='567sA'><style id='567sA'><dir id='567sA'><q id='567sA'></q></dir></style></legend>
          • <small id='567sA'></small><noframes id='567sA'>

              <tfoot id='567sA'></tfoot>
            • <i id='567sA'><tr id='567sA'><dt id='567sA'><q id='567sA'><span id='567sA'><b id='567sA'><form id='567sA'><ins id='567sA'></ins><ul id='567sA'></ul><sub id='567sA'></sub></form><legend id='567sA'></legend><bdo id='567sA'><pre id='567sA'><center id='567sA'></center></pre></bdo></b><th id='567sA'></th></span></q></dt></tr></i><div id='567sA'><tfoot id='567sA'></tfoot><dl id='567sA'><fieldset id='567sA'></fieldset></dl></div>

                  本文介绍了为什么更新一个对象的属性会改变另一个对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我正在通过 ajax 将 JSON 数据加载到一个对象中,将该对象复制到新对象(initData 和 newData).当我更改 newData 的属性时,initData 的属性也会更改.为什么会这样?

                  I'm loading JSON data into an object via ajax, copying that object to new objects (initData and newData). When I change the property of newData, the property of initData also changes. Why is this happening?

                  var initData = {};
                  var newData = {};    
                  
                  function load_data(NDB_No){
                      $.getJSON(('scripts/jsonencode.php?q=' + NDB_No), function(data) {
                  
                          for (prop in data){
                              initData[prop] = data[prop];
                              newData[prop] = data[prop];
                          }
                  
                      console.log('init data: ' + initData.properties.Protein); // "init data: 0.259"
                      console.log('new data: ' + newData.properties.Protein); // "new data: 0.259"
                  
                       var n = parseFloat(newData.properties.Protein);
                       newData.properties.Protein = n+1;
                  
                      console.log('init data: ' + initData.properties.Protein + 'new data: ' + newData.properties.Protein); 
                      // "init data: 1.259 new data: 1.259"
                      // why are these the same when I only updated newData object?
                  
                  
                      });
                  
                  
                  }
                  

                  推荐答案

                  看起来 data[prop] 是一个对象(因为你后面提到 newData.properties.Protein).对象总是通过引用传递,变量只是指向它的指针.

                  It looks like data[prop] is an object (since you are later referring to newData.properties.Protein). Objects are always passed by reference, with the variable just being a pointer to it.

                  由于您首先获取的是 JSON,因此您的对象是支持 JSON 的,因此您可以使用它来克隆"对象:

                  Since you're getting JSON in the first place, your object is JSON-able, so you can use that to "clone" the object:

                  $.getJSON(...,function(data) {
                      initData = JSON.parse(JSON.stringify(data));
                      newData = JSON.parse(JSON.stringify(data));
                  });
                  

                  这将确保对象是分开的.还有其他方法可以做到这一点,但是这种方法通过使用内置方法避免了手动递归(总是更快)

                  This will ensure that the objects are separate. There are other ways to do this, but this one avoids the manual recursion by using built-in methods (always faster)

                  这篇关于为什么更新一个对象的属性会改变另一个对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))

                  <tfoot id='qiHvO'></tfoot>
                    <bdo id='qiHvO'></bdo><ul id='qiHvO'></ul>
                      <i id='qiHvO'><tr id='qiHvO'><dt id='qiHvO'><q id='qiHvO'><span id='qiHvO'><b id='qiHvO'><form id='qiHvO'><ins id='qiHvO'></ins><ul id='qiHvO'></ul><sub id='qiHvO'></sub></form><legend id='qiHvO'></legend><bdo id='qiHvO'><pre id='qiHvO'><center id='qiHvO'></center></pre></bdo></b><th id='qiHvO'></th></span></q></dt></tr></i><div id='qiHvO'><tfoot id='qiHvO'></tfoot><dl id='qiHvO'><fieldset id='qiHvO'></fieldset></dl></div>

                    • <small id='qiHvO'></small><noframes id='qiHvO'>

                          <tbody id='qiHvO'></tbody>

                          <legend id='qiHvO'><style id='qiHvO'><dir id='qiHvO'><q id='qiHvO'></q></dir></style></legend>