Sign in
Log inSign up

How to copy one object's values to another same type object, if that object contains some arrays?

Default profile photo
Anonymous
·Feb 12, 2019

Let, I have a class

class Node {
    constructor() {
        this.a = [];
        this.b = [];
    }
}

Now, I creates two objects

let obj1 = new Node();
let obj2 = new Node();

assigned value in obj1

for(let i = 0; i <= 5; i++) {
    obj1[i] = i;
}

Now if I copy obj1 to obj2 , and make any change in obj1 will reflect to obj2. How should I copy this type of object?