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
...
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")) {
// ...
}
I've a workaround using find :
const CARS = ['RENAULT', 'FORD']
const SUV = ['PORSCHE', 'LAMBUGININININ']
const SCOOTER = ['HONDA']
function select(vehicule) {
switch (vehicule) {
// Alternative use `case undefined: return 'precatch undefined`'
case vehicule !== undefined && CARS.find(_ => _ === vehicule): return 'CARS';
case vehicule !== undefined && SUV.find(_ => _ === vehicule): return 'SUV'
case vehicule !== undefined && SCOOTER.find(_ => _ === vehicule): return 'SCOOTER';
default: return 'noop';
}
}
['', null, undefined, 'WHATEVER', 'HONDA', 'RENAULT']
.forEach(_ => console.log(_, ' - ', select(_)))
/**
Expected result:
- noop
null - noop
undefined - noop
WHATEVER - noop
HONDA - SCOOTER
RENAULT - CARS
**/
Not sure how this method is discouraged but sounds not so bad.
case, and there’s little reason not to iterate over the arrays/return values as well. It’s just not a good use for a switch in JS; no sense trying to shoehorn it in.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.
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.
input will be of the same type for each case — as opposed to being discriminated. See TypeScript Playground.new keyword.It's better to use if statements combined with type guards instead.
switch, the latter is possible but rather odd....might be? Is theArrayin your example literally theArraytype, so you are wanting to switch on the type?