在javascript中没有对象引用的情况下将对象数组复制到另一个数组(深度复制) [英] Copying of an array of objects to another Array without object reference in javascript(Deep copy)

查看:73
本文介绍了在javascript中没有对象引用的情况下将对象数组复制到另一个数组(深度复制)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一种情况,我需要将Objects(Main数组)的数组复制到另一个Temp数组,如果我对Main数组进行了任何修改,则基本上不应该具有对象引用,因此它不应反映在Temp数组中,因此我将独立保存副本.

I have a scenario where i need to copy the array of Objects(Main array) to another Temp array which should not have object reference basically if i make any modification to Main array it should not reflect in the Temp array so that i will preserve the copy independently.

我从堆栈溢出中使用了其中一个代码段,这部分执行部分操作,就像我从Main数组中删除所有对象时,temp数组仍保留该值,但是当我在main数组中进行一些修改并单击cancel按钮时,iam删除使用array.Removeall();主数组中的所有对象;但是修改仍然存在于Temp数组中,这意味着该对象具有引用.

I have used one of the code snippet from stack overflow this one does partially like if i delete all objects from the Main array the temp array still hold the value but when i do some modifications in main array and click cancel button iam removing all objects from the main array using array.Removeall(); but the modification still exist in Temp array so which means that object having a reference.

clone: function (existingArray) {
  var newObj = (existingArray instanceof Array) ? [] : {};
  console.debug('newObj value is ' + newObj);
  for (i in existingArray) {
    console.debug('i value is' + i);
    if (i == 'clone') continue;
    console.debug('existingArray[i] value ' + existingArray[i]);
    if (existingArray[i] && typeof existingArray[i] == "object") {

      newObj[i] = this.clone(existingArray[i]);
    } else {
      console.debug('in else part ' + existingArray[i]);
      newObj[i] = existingArray[i];
    }
  }
  return newObj;
}

我的对象结构就像

使用淘汰赛框架

iam using knockout framework.

newObjectCreation = function (localIp, RemoteIp, areaId) {
  this.localIP = ko.observable(localIp);
  this.remoteIP = ko.observable(RemoteIp);
  this.areaId = ko.observable(areaId);
};

template.ProtocolArray.push(new newObjectCreation('', '', '')); // to create default row

在这方面请帮助我. 预先感谢.

please help me in this regard. Thanks in advance.

推荐答案

让我理解:您不希望拥有一个新数组,而是想为数组本身中存在的所有对象创建一个新实例吗?因此,如果您修改temp数组中的对象之一,那么更改不会传播到主数组中?

Let me understand: you don't want just have a new array, but you want to create a new instance for all objects are present in the array itself? So if you modify one of the objects in the temp array, that changes is not propagated to the main array?

如果是这种情况,则取决于您在主数组中保留的值.如果这些对象是简单对象,并且可以使用JSON进行序列化,那么最快的方法是:

If it's the case, it depends by the values you're keeping in the main array. If these objects are simple objects, and they can be serialized in JSON, then the quickest way is:

var tempArray = JSON.parse(JSON.stringify(mainArray));

如果您有更复杂的对象(例如由您自己的构造函数创建的实例,html节点等),则需要一种专门的方法.

If you have more complex objects (like instances created by some your own constructors, html nodes, etc) then you need an approach ad hoc.

如果newObjectCreation上没有任何方法,则可以使用JSON,但是构造函数将不同.否则,您必须手动进行复制:

If you don't have any methods on your newObjectCreation, you could use JSON, however the constructor won't be the same. Otherwise you have to do the copy manually:

var tempArray = [];
for (var i = 0, item; item = mainArray[i++];) {
    tempArray[i] = new newObjectCreation(item.localIP, item.remoteIP, item.areaId);
}

这篇关于在javascript中没有对象引用的情况下将对象数组复制到另一个数组(深度复制)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆