So let's say there is an object a
:
let a = {
b: {
c: {
d: {
e: null
}
}
}
}
The task is to create a method which would be assigning a value to a
-object's property by property's path that will be passed into method as string.
So expected result is:
Method
let magicMethod = ({prop, value}) => {
/*magic here*/
}
Call:
magicMethod({prop: 'b.c.d.e', value: true})
Result (console.log(a)):
{
b: {
c: {
d: {
e: true
}
}
}
}
It should work for all deepness levels, so call magicMethod({prop: 'b.c', value: true})
should modify a
-object to:
{
b: {
c: true
}
}
Is there a way to achieve such behavior via recursion or something else?
So far, i did manage to implement it with this approach:
But it is not flexible and, to be honest, i don't like it at all :) I have a feeling that there are some smarter solutions.