3

How can I use an array as case parameter in switch case?

switch ("value")
  case ArrayOfStrings // check every array item to be match with value
    ...
Mohammadreza
  • 83
  • 1
  • 1
  • 10
  • 3
    Do you want to check if a *specific* array is passed or if the array constructor function is passed? The former is not possible in a `switch`, the latter is possible but rather odd. – VLAZ May 27 '20 at 11:39
  • 1
    Can you give a more detailed example of what you are trying to achieve, such as an example of what `...` might be? Is the `Array` in your example literally the `Array` type, so you are wanting to switch on the type? – lurker May 27 '20 at 11:40
  • do yoi have a use case for it? what about the other cases? – Nina Scholz May 27 '20 at 11:41
  • What is the input for your switch? Is that an array, or are you trying to match one value against an array of values? – Ivar May 27 '20 at 11:41
  • Is [this](https://stackoverflow.com/questions/13207927/switch-statement-multiple-cases-in-javascript) what you are looking for? – Ivar May 27 '20 at 11:43
  • what is in the other cases? – Nina Scholz May 27 '20 at 11:46
  • 2
    Clean is quite subjective. I find that way very clean. Maybe your solution would be to _not_ use a switch statement, but you haven't provided enough detail for us to determine that. This looks a bit like an [XY problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem). – Ivar May 27 '20 at 11:46

3 Answers3

6

No.

This

switch ("value") {
  case ArrayOfStrings // check every array item to be match with value
    ...

does not work, because the value of switch and the value of case is checked with a Identity/strict equality operator === comparison.

It is not possible to check a value agains a value of the array.

Any other construction, like

switch (true) {
  case ArrayOfStrings.includes("value"):  // check every array item to be match with value

would work, but if you have only to check a single value and not other constraints, you better take

if (ArrayOfStrings.includes("value")) {
    // ...
}
Neuron
  • 5,141
  • 5
  • 38
  • 59
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
-1

Well first I would suggest using if with something like Array.isArray:

if (Array.isArray(value)) {
  // check array items
}

But, if you really want/need to check by comparing to Array constructor:

const check = (value) => {
  switch (value.constructor) {
    case Array:
      console.log('array');
      break;

    // For example
    case Number:
      console.log('number');
      break;
  }
}

check(7); // 'number'
check([]); // 'array'

I would really discourage from using this approach though.

Ján Jakub Naništa
  • 1,880
  • 11
  • 12
-3

In theory, you could do that to distinguish between different constructors.

class ArrayOfStrings extends Array<string> {};
class ArrayOfNumbers extends Array<number> {};

function main(input: ArrayOfStrings | ArrayOfNumbers): void {
  switch (input.constructor) {
    case ArrayOfStrings:
      console.log('It was an array of strings!');
      break;
    default:
       console.log('It was something else');
  }
}

main(new ArrayOfStrings('foo', 'bar')); // Logs 'It was an array of strings!'
main(new ArrayOfNumbers(1, 2)); // Logs 'It was something else'

However, this method has limitations.

  1. It doesn't work with subclassing.
  2. input will be of the same type for each case — as opposed to being discriminated. See TypeScript Playground.
  3. You need to create your arrays by using the new keyword.

It's better to use if statements combined with type guards instead.

Karol Majewski
  • 23,596
  • 8
  • 44
  • 53