Optional chainning is a powerfull feature when it comes to access data like
const options = cache.[server]?.[channel]?.[service]
Now when writing data we generally need to do something like
cache[server] ??= {}
cache[server][channel] ??= {}
cache[server][channel][service] = options
Is there something like optional chaining that make asignment like this lighter (maybe in one line)? Something like :
cache[server]??{}:[channel]??{}:[service]=options
In this case adding {}
or []
just after would permit to indicate what's expected to assign if nullish.
As proposed by @Bergi, the nearest way of doing this is to write like that:
((cache[server] ??= {})[channel] ??= {})[service] = options
You can use parenthesis to transform the
cache[server] ??= {}
cache[server][channel] ??= {}
cache[server][channel][service] = options
into a single expression
((cache[server] ??= {})[channel] ??= {})[service] = options
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments