【问题标题】:Deep cloning/copy for javascript objectsjavascript对象的深度克隆/复制
【发布时间】:2015-01-08 18:02:33
【问题描述】:

寻找深度复制包含嵌套和循环结构的对象的方法,这些(1234)在循环引用和原型继承方面都没有完美的解决方案.

我在这里写了自己的implementation。它做得好还是算作更好的解决方案?

 /*
    a function for deep cloning objects that contains other nested objects and circular structures.
    objects are stored in a 3D array, according to their length (number of properties) and their depth in the original object.
                                    index (z)
                                         |
                                         |
                                         |
                                         |
                                         |
                                         |                      depth (x)
                                         |_ _ _ _ _ _ _ _ _ _ _ _
                                        /_/_/_/_/_/_/_/_/_/
                                       /_/_/_/_/_/_/_/_/_/
                                      /_/_/_/_/_/_/...../
                                     /................./
                                    /.....            /
                                   /                 /
                                  /------------------
            object length (y)    /
*/


function deepClone(obj) {
    var i = -1, //depth of the current object relative to the passed 'obj'
        j = 0;  //number of the object's properties
    var arr = new Array(); //3D array to store the references to objects
    return clone(obj, arr, i, j);
}

function clone(obj, arr, i ,j){
    if (typeof obj !== "object") {
        return obj;
    }

    var result = Object.create(Object.getPrototypeOf(obj)); //inherit the prototype of the original object
    if(result instanceof Array){
        result.length = Object.keys(obj).length;
    }

    i++; //depth is increased because we entered an object here
    j = Object.keys(obj).length; //native method to get the number of properties in 'obj'
    arr[i] = new Array(); //this is the x-axis, each index here is the depth
    arr[i][j] = new Array(); //this is the y-axis, each index is the length of the object (aka number of props)
    //start the depth at current and go down, cyclic structures won't form on depths more than the current one
    for(var depth = i; depth >= 0; depth--){
        //loop only if the array at this depth and length already have elements
        if(arr[depth][j]){
            for(var index = 0; index < arr[depth][j].length; index++){
                if(obj === arr[depth][j][index]){
                    return obj;
                }
            } 
        }
    }

    arr[i][j].push(obj); //store the object in the array at the current depth and length
    for (var prop in obj) {
        result[prop] = clone(obj[prop], arr, i, j);
    }

    return result;
}

【问题讨论】:

  • @Ja͢ck 之前看过。我相信它不处理圆形结构,对吧?
  • 另外,特殊情况需要手动实现(如日期、数组等)
  • 就在第一段,描述结构化克隆算法的页面提到了循环图。基本上这个问题是经过充分研究的图遍历问题。
  • @akonsu 我正在阅读它的伪implementation。你认为哪一个会更快? 3D 阵列,当我接近它时,还是一张地图,当他们接近时?

标签: javascript object clone


【解决方案1】:

你能试试下面的简单逻辑吗?将 js 对象转换为新字符串并再次转换为对象。

例如:

var car = {type:"Fiat", model:"500", color:"white"};
var cloneStr = new String(JSON.stringify(car));
var carClone = JSON.parse(cloneStr);
carClone.type = "Maruthi";
alert(JSON.stringify(car)) ;
alert(JSON.stringify(carClone)) ;

【讨论】:

    猜你喜欢
    • 2010-09-09
    • 2012-06-05
    • 2013-09-06
    • 2013-10-11
    • 2018-03-21
    • 2019-03-25
    • 1970-01-01
    相关资源
    最近更新 更多