Home > Blockchain >  Combine multiple "equals ... or"
Combine multiple "equals ... or"

Time:12-21

  var group = 4;
  var level = [1,2];
  var options = 'ng;nk';

  var arrayWithCorrectGroup = shuffledArray.filter(innerArray => innerArray[0] === group);
  var arrayWithCorrectLevels = arrayWithCorrectGroup.filter(x => x[1] === level[0] || x[1] === level[1] );
  var arrayWithCorrectValues = arrayWithCorrectLevels.filter(x => x[3].includes(...options.split(';')));

When using .includes as seen in arrayWithCorrectValues, you can use the three dots to loop through all options in the array. I want to know if there is a similar way for the equals or in the arrayWithCorrectLevels. It's possible to have an array with 10 levels. I don't want to write x[1] === level[n] ten times.

CodePudding user response:

There are several ways to check if x[1] is equal to any element of the level array.

Example one:

level.includes(x[1]);

Example two:

level.some(item => item === x[1]);
  • Related