benhowdle89
benhowdle89

Reputation: 37474

ES6 shorthand object key checking

Curious to know if there's any part of ES6 that makes these kind of checks a little more concise:

componentWillReceiveProps(nextProps) {
    if(nextProps && nextProps.filterObj && nextProps.filterObj.area){
        // go ahead
    }
}

Upvotes: 12

Views: 4729

Answers (1)

Bergi
Bergi

Reputation: 664620

No, no existential operator has made it into ES6; it is still discussed however.

You can use any of the existing methods, of course, like

if ( ((nextProps||{}).filterObj||{}).area ) {
    // go ahead
}

Also you can try destructuring and default values:

function componentWillReceiveProps({filterObj: {area} = {}} = {}) {
    if (area) {
        // go ahead
    }
}

Upvotes: 12

Related Questions