I have a function that returns an object like the following:
{
data: {key1: value1, ...},
errors: [...]
}
I can extract key1 with the following:
const { data: { key1 }} = myFunction()
However, sometimes data is undefined which causes the destructuring to fail.
I've looked at the destructuring examples and haven't been able to figure out how to pull out key1 when data might be undefined.
Is there a way to assign a default value of {} to data when performing the destructuring so that it doesn't fail?
You could take a default object.
const
myFunction = () => ({}),
{ data: { key1 } = {} } = myFunction();
console.log(key1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With