0

I have 2 list of values I need to compare, to separate variables, before I call a function so variable args[0] needs to be found in the list (orange, apple, banana, grape, strawberry, etc...) and variable args[1] needs to be found in the list (bike, car, motorcycle, boat, etc...)

I need to find a good value for both before proceeding to the next step.

I could use a nasty looking if statement such as

if ( (args[0] == "orange" || args[0] == "apple"|| and so on) && (args[1]) == "bike" || agrs[1] == "car" || and so on) ) 

but that can get really ugly to work with really fast. Is there a better way to skin the cat?

I don't see any function that I can use if ( (args[0] in (apple, orange, etc...) && (args[1] in ...)).

Cristian Ciupitu
  • 20,270
  • 7
  • 50
  • 76
user3577459
  • 51
  • 1
  • 1
  • 2

1 Answers1

0

Use Array.prototype.indexOf():

var list1 = ['orange', 'apple'];
var list2 = ['bike', 'car'];

if(list1.indexOf(args[0]) > -1 && list2.indexOf(args[1]) > -1){

}

Notes:

  • This uses strict equality (same as ===).
  • It is case sensitive, so Orange will not match orange.
MrCode
  • 63,975
  • 10
  • 90
  • 112