Create deep copy in angular 2

This question already has an answer here:

  • What is the most efficient way to deep clone an object in JavaScript? 57 answers

  • 只需使用以下功能:

    /**
     * Returns a deep copy of the object
     */
    
    public deepCopy(oldObj: any) {
        var newObj = oldObj;
        if (oldObj && typeof oldObj === "object") {
            newObj = Object.prototype.toString.call(oldObj) === "[object Array]" ? [] : {};
            for (var i in oldObj) {
                newObj[i] = this.deepCopy(oldObj[i]);
            }
        }
        return newObj;
    }
    

    Try to use the Lodash.js . Because angular 2 does not have any method for deep copy . for reference see :https://lodash.com/docs#cloneDeep

    or You can use this javascript function

    var copy = Object.assign({}, myObject);
    
    链接地址: http://www.djcxy.com/p/6952.html

    上一篇: JavaScript:如何制作一个对象的副本?

    下一篇: 以角度2创建深层复制