How to match string with multiple variables?

Viewed 145

I have a situation , where i have to render some component (e.g. ) in multiple pages of my app , so i have to check if current path matches the paths i provide via CONSTANTS .

if(this.props.location.pathname.match(CONST1) || this.props.location.pathname.match(CONST2)) {
    render component ...
  }

Yes this works well , but i think there is more elegant solution maybe with Regexp . Any advice ? I have also tried this but it didn't work for my situation

if(this.props.location.pathname.match(CONST1 || CONST2)){
    render component ...
  }
1 Answers

If we have:

let path = this.props.location.pathname;

Then we can use ES5(!) some (there also is every):

if( [CONST1, CONST2].some(x=>path.match(x)) ) {...};
Related