I am trying to destructure the a response from the API. response looks something like below.
Only a
and b
will be used in the program.
response = {
data: {
someData: {
a:1,
b:2
}
}
}
destructured
const {data: { someData: { a,b } } } = response
Now if data doesn't exist in the response, there are chances that app might break. I want set default value to data
and someData
. Is there a way to set the default value?
Tried : but throwing lint error as unexpected token
const {data= {}: { someData = {}: { a,b } } } = response
The default value goes at the end, not immediately after the variable.
And it needs to have the someData:
nested object to provide the default there.
const response = {};
const {data: {someData: { a,b } } = {someData: {a:0, b:0}} } = response;
console.log(a, b);
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