-3

I am pretty new to Angular and I am writing a simple if statement to check if my property isn't one 10 things. Is there a Not In or filter from enums or from a list?

public type: string;
if(type === '1' || type === '2' || type === '3') || type === '4'){
// do something
} else {
//do something else
}

I am looking for something simple if we can use like

if(type not in (1,2,3))

What is the best way to check the scenario like this against the string?

georgeawg
  • 48,608
  • 13
  • 72
  • 95
Learn AspNet
  • 1,192
  • 3
  • 34
  • 74

2 Answers2

3
!['1', '2', '3', '4'].some(val => val === type);

or

!['1', '2', '3', '4'].includes(type);
Adrian Brand
  • 20,384
  • 4
  • 39
  • 60
1

You can use Array.includes() for the same. The includes() method determines whether an array includes a certain value among its entries, returning true or false as appropriate.

Ref - MDN

if(['1','2','3','4'].includes(type)) {
   ...
} else {
   ...
}
Nithin Kumar Biliya
  • 2,763
  • 3
  • 34
  • 54
  • The provided answer was flagged for review as a Low Quality Post. Here are some guidelines for [How do I write a good answer?](https://stackoverflow.com/help/how-to-answer). This provided answer may be correct, but it could benefit from an explanation. Code only answers are not considered "good" answers. From [review](https://stackoverflow.com/review). – MyNameIsCaleb Sep 22 '19 at 03:43