When the value of js object encounters undefinded, how do I stop taking values?

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?

Mar.20,2021

var key = (val.o1 | | val.o2 | | {}). Key | | null;


consider the following code if you can ignore es6 compatibility:

const {o1:{key:key1}={},o2:{key:key2}={}} = val
const key = key1 || key2

Brother, which if statement can't go at all? the return value of an object typeof should be object. You can use instanceof


Object.values(val)[0]['key']
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1b3e8bf-2c42c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1b3e8bf-2c42c.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?