I am receiving a nested object as a response from my API which looks like this:
myObject = {
name: 'client name',
address: {
building: 'building name',
block: 'block name',
street: 'street name'
}
I am currently destructing it by
const { name, address: {building, block, street}} = myObject;
The code above works fine when myObject has the address object, but it's possible to get a response with the address object as null. like:
myObject = {
name: 'client name',
address: null,
}
in that case, I get an error saying: Cannot read property 'building' of null
I have tried destructing like below
const { name, address: {building, block, street} = {}} = myObject;
It only works if I get the address as undefined.
I have accomplished this by destructuring in multiple stages, but would rather do it in a single stage if possible.
nullandundefined?nulladdressasundefined