I have a variable and I want to destructure it
const myFunc = ({name}) => {
console.log(name)
}
myFunc({name: 'hey'})
But what if the variable is not there?
const myFunc = ({name}) => {
console.log(name)
}
myFunc({name2: 'hey'})
It just says undefined
, which is fine
But what if I am destructuring more?
const myFunc = ({name, structure: {type}}) => {
console.log(name)
console.log(type)
}
myFunc({name: 'hey', structure : {type: 'hay'}})
Ok, but what if the parameter is not there?
const myFunc = ({name, structure: {type}}) => {
console.log(name)
console.log(type)
}
myFunc({name2: 'hey', structure2 : {type: 'hay'}})
Then it gives an (expected) error
The question is, how can I cope with these situations while destructuring in the same sentence? Or am I forced to destructure in 2 steps?