the question is as follows:
the other party normally passed me an object. After receiving it, it is as follows
var val={
o1:{
key:1
},
o2:{
key:2
}
}
but when an error occurs, the default value is returned. After I receive it, it is as follows
var val={
o2:{
key:2
}
}
what I want is that if I take the default value passed to me when an error occurs, I think it"s more elegant to write this [but report an error]
var key=val.o1.key || val.o2.key;//undefined
but I need to determine whether o1 is undefined, as follows
if(typeof val.o1 === "undefined"){
key=val.o2.key;
}else{
key=val.o1.key;
}
is there any effective but elegant way to var key=val.o1.key | | val.o2.key;, if o1 is not defined, go directly to the following value without reporting an error?