0

I tried again and again to look for a simple way to duplicate a threaded object but unfortunately without success.

Each time I tried to duplicate the threaded object containing additional objects, the chained objects were linked to objects under the original object and were not replicated as expected and also contained links to the original objects in the original object, for example:

let obj1 = { a: 0 , b: { c: 0}};
let obj2 = Object.assign({}, obj1);
console.log(JSON.stringify(obj2)); // { a: 0, b: { c: 0}
  
obj1.a = 1;
console.log(JSON.stringify(obj1)); // { a: 1, b: { c: 0}}
console.log(JSON.stringify(obj2)); // { a: 0, b: { c: 0}}
  
obj2.a = 2;
console.log(JSON.stringify(obj1)); // { a: 1, b: { c: 0}}
console.log(JSON.stringify(obj2)); // { a: 2, b: { c: 0}}
  
obj2.b.c = 3;
console.log(JSON.stringify(obj1)); // { a: 1, b: { c: 3}}
console.log(JSON.stringify(obj2)); // { a: 2, b: { c: 3}}

Thank you very much for your help!

Sagi Nadav
  • 295
  • 1
  • 3
  • 14
  • After searching, I also saw the solutions to this question, unfortunately most of the solutions offered there do not answer my question with an emphasis on simplicity and are not used in other libraries like JQuery, thank you! – Sagi Nadav Feb 20 '18 at 17:30
  • Does https://stackoverflow.com/questions/4459928/how-to-deep-clone-in-javascript help? – fjc Feb 20 '18 at 17:32
  • @fjc By the way it looks, there is no simple and universal solution to my problem. In the meantime I am still optimistic in the hope of a solution as I am looking for :( – Sagi Nadav Feb 20 '18 at 17:39
  • @Sagi Nadav: there are lots of answers for every use case in the linked questions. Please specify what exactly is wrong about them. – georg Feb 20 '18 at 17:47
  • @georg Indeed solutions have a lot, a simple solution based on a generic command unfortunately do not. I'll go through the subject a little more tomorrow to get to the office, and later I'll update my question, thank you all! – Sagi Nadav Feb 20 '18 at 18:02

1 Answers1

0

Getting back to the link I posted as a comment, it works with the function supplied (if I understand your problem correctly):

function clone(item) {
    if (!item) {
        return item;
    } // null, undefined values check

    var types = [Number, String, Boolean],
        result;

    // normalizing primitives if someone did new String('aaa'), or new Number('444');
    types.forEach(function (type) {
        if (item instanceof type) {
            result = type(item);
        }
    });

    if (typeof result == "undefined") {
        if (Object.prototype.toString.call(item) === "[object Array]") {
            result = [];
            item.forEach(function (child, index, array) {
                result[index] = clone(child);
            });
        } else if (typeof item == "object") {
            // testing that this is DOM
            if (item.nodeType && typeof item.cloneNode == "function") {
                var result = item.cloneNode(true);
            } else if (!item.prototype) { // check that this is a literal
                if (item instanceof Date) {
                    result = new Date(item);
                } else {
                    // it is an object literal
                    result = {};
                    for (var i in item) {
                        result[i] = clone(item[i]);
                    }
                }
            } else {
                // depending what you would like here,
                // just keep the reference, or create new object
                if (false && item.constructor) {
                    // would not advice to do that, reason? Read below
                    result = new item.constructor();
                } else {
                    result = item;
                }
            }
        } else {
            result = item;
        }
    }

    return result;
}


let obj1 = {a: 0, b: {c: 0}};
let obj2 = Object.assign({}, obj1);
console.log(JSON.stringify(obj2)); // { a: 0, b: { c: 0}

obj1.a = 1;
console.log(JSON.stringify(obj1)); // { a: 1, b: { c: 0}}
console.log(JSON.stringify(obj2)); // { a: 0, b: { c: 0}}

obj2.a = 2;
console.log(JSON.stringify(obj1)); // { a: 1, b: { c: 0}}
console.log(JSON.stringify(obj2)); // { a: 2, b: { c: 0}}

obj2.b.c = 3;
console.log(JSON.stringify(obj1)); // { a: 1, b: { c: 3}}
console.log(JSON.stringify(obj2)); // { a: 2, b: { c: 3}}

console.log("Clone");
obj1 = {a: 0, b: {c: 0}};
let obj3 = clone(obj1);
console.log(JSON.stringify(obj3)); // { a: 0, b: { c: 0}

obj1.a = 1;
console.log(JSON.stringify(obj1)); // { a: 1, b: { c: 0}}
console.log(JSON.stringify(obj3)); // { a: 0, b: { c: 0}}

obj3.a = 2;
console.log(JSON.stringify(obj1)); // { a: 1, b: { c: 0}}
console.log(JSON.stringify(obj3)); // { a: 2, b: { c: 0}}

obj3.b.c = 3;
console.log(JSON.stringify(obj1)); // { a: 1, b: { c: 0}}
console.log(JSON.stringify(obj3)); // { a: 2, b: { c: 3}}

All the credit goes to How to Deep clone in javascript

fjc
  • 5,590
  • 17
  • 36