Object dynamic property with or
somonek
somonek at gmail.com
Thu Jun 15 07:20:54 UTC 2017
probably just an alternative of doing
let myProp = myObj['propertyThree'] || myObj['propertyTwo']
On Thu, Jun 15, 2017 at 9:17 AM, somonek <somonek at gmail.com> wrote:
> 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