我有一个场景,我需要将对象数组(主数组)复制到另一个 Temp 数组,如果我对 Main 数组进行任何修改,它基本上不应该有对象引用,它不应该反映在 Temp 数组中,这样我就可以保留独立复制。
我使用了堆栈溢出的代码片段之一,这部分就像我从主数组中删除所有对象一样,临时数组仍然保留该值,但是当我在主数组中进行一些修改并单击取消按钮时,我会从中删除所有对象使用 array.Removeall() 的主数组;但修改仍然存在于 Temp 数组中,这意味着该对象具有引用。
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;
}
我的对象结构就像
我使用淘汰赛框架。
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
请在这方面帮助我。提前致谢。