Say I have an object named foo, and I would like to perform the following operation:
foo.bar.baz.qwer.asdf = 5;
The issue is that bar may not exist, but if it does exist, then baz inside of it may not exist, etc.
I thought I would be able to do something like this:
foo?.bar?.baz?.qwer?.asdf = 5, but alas, that sort of chaining only seems to work with retrieving values, not setting values.
The desired behavior is that if something like baz already exists, then that object is just accessed (and its existing members are maintained), but if baz does not exist, then that property is created on the fly.
That is, let's say only foo.bar existed. Then after the above assignment operation, the resulting object would be:
foo {
bar: {
baz: {
qwer: {
asdf: 5
}
}
}
}
So my question is: can this be done with existing language syntax constructs, or must a helper function be created? If so, does this sort of function have a name that I could search for and reference?