Object dynamic property with or

somonek somonek at gmail.com
Thu Jun 15 07:17:27 UTC 2017


Hi everyone,

consider the following example:

const myObj = {
    propertyOne: 'someValue',
    propertyTwo: 'another value',
};

suppose I have to get a non existing property dynamically with a
fallback on another one:

let myProp = myObj['propertyTwo'];
if (myObj['propertyThree']) {
    myProp = myObj['propertyThree'];
}

wouldn't it be nice to have a quicker way of doing that like this?

const myProp = myObj['propertyThree' || 'propertyTwo'];
this will return undefined of course but some other operator could be used like:
let myProp = myObj['propertyThree' ?? 'propertyTwo'];
let myProp = myObj['propertyThree' ?? 'propertyFour' ?? 'propertyTwo'];

any thoughts?

cheers,
Serghei


More information about the es-discuss mailing list