How do I set object references of one type with another object reference in a deeply nested object?
// Following is the class
class Ab{ a: number; }
let x = new Ab();
x.a = 10;
// Now I have an object something like this
let obj = {
b: 'hello',
c: new Ab(),
d: {
x: 5,
y: new Ab(),
z: {
p: new Ab()
}
}
}
Now I should be able to change all the references of new Ab() set to x
object.
let obj = {
b: 'hello',
c: x, // reassign the x reference
d: {
x: 5,
y: x,
z: {
p: x
}
}
}
I think I have to go with deep cloning the object. I was using lodash
lodash.cloneDeepWith(obj, val => {
// please do not worry/consider these logic this condition is working
if (Object.getPrototypeOf(val) === tc) {
// Actual problem is here
// reassigning is not changing the original reference of obj
val = x;
}
});
Are there any solutions out there which are faster and easier? Any help is appreciated.