简体   繁体   中英

Javascript equivalent of CASE expression

In SQL and PL/SQL we can have a CASE expression like this:

y := case x
     when 1 then 'foo'
     when 5 then 'bar'
     when 7 then 'baz'
     else 'wak'
     end;

The simplest expression of this type I can think of in javascript is to use nested ternary operators, like this:

y = (x==1)
    ? "foo"
    :( (x==5)
       ? "bar"
       :( (x==7)
          ? "baz"
          : "wak"
          )))

Is there a simpler/clearer way to form this sort of thing as an expression ?

NOTE: I know about the switch statement , but here I'm looking for an expression .

Use a switch statement:

switch(x) {
  case 1:
    y = 'foo';
    break;
  case 5:
    y = 'bar';
    break;
  case 7:
    y = 'baz';
    break;
  default:
    y = 'wak';
}

Alternatively, if you absolutely need an expression, you could use an object as a key-value map, like this:

y = {
    1: 'foo',
    5: 'bar',
    7: 'baz',
  }[x] || 'wak';

you can use a key-value map

y = {1: "foo", 5: "bar", 7: "then"}[x] || "wak";

or if the map is bigger or you use falsy values, ...

var map = {
    1: "foo",
    5: "bar",
    7: "then"
};

y = x in map? map[x]: "wak";

Your key to search is switch

The switch statement evaluates an expression, matching the expression's value to a case clause, and executes statements associated with that case.

switch(x) {
    case 1:
        //code  
        break;
    case 5:
        //code  
        break;
    // more cases
    default:
       // code  
}

Use object literal :

v = { ...  }[expression];


y = {1: 'foo', 5: 'bar', 7: 'baz'}[x] || 'wak';

Caution: if you need the else clause as shown none of the case values should be a Javascript falsy.

switch is the typical translation for case , but it's not an expression. You could assign to y inside the statement, though. Note that you'll need break to prevent the case from bubbling on and return the next result:

 var y, x = 7; switch (x) { case 1: y = 'foo'; break; case 5: y = 'bar'; break; case 7: y = 'baz'; break; default: y = 'wak'; break; } console.log(y); 

Given that you are looking for an expression , you could make a function:

 function getY(x) { switch (x) { case 1: return 'foo'; case 5: return 'bar'; case 7: return 'baz'; default: return 'wak'; } } y = getY(7); console.log(y); 
Alternatively, you can use an arrow function :

 y = ((x) => { switch (x) { case 1: return 'foo'; case 5: return 'bar'; case 7: return 'baz'; default: return 'wak'; }})(7); console.log(y); 

There is also a flavor in between using an Immediately-Invoked Function Expression (IIFE) , but in essence it's the same as the other two.

I'd probably suggest a switch statement as shown here

var y;
switch(expression) {
    case 1:
    y = 'foo';
    break;
case 5:
    y = 'bar';
    break;
default:
    y = 'wak';
}

https://www.w3schools.com/js/js_switch.asp

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM