admin管理员组

文章数量:1406913

I just try to make a switch by two values.

switch ({'a': val_a,'b': val_b}){
  case ({'x','y'}):
    "some code here"
    break;
}

and this not working... any help? thanks!

I just try to make a switch by two values.

switch ({'a': val_a,'b': val_b}){
  case ({'x','y'}):
    "some code here"
    break;
}

and this not working... any help? thanks!

Share asked Mar 15, 2022 at 6:50 flap13flap13 4111 gold badge4 silver badges15 bronze badges 2
  • ...what kind of equality parision should happen in the expression above, in your opinion, i wonder ( aka, isEqual("{'a': val_a,'b': val_b}", {'x','y'})...? :/ – Vovan_Super Commented Mar 15, 2022 at 7:16
  • To begin with this is a JSON object, not a JavaScript object. JavaScript objects don't have strings for keys. { a: val_a, b: val_b } is a JavaScript object. { 'a': val_a, 'b': val_b } is a JSON object. You would either need this parsed if it is indeed JSON with JSON.parse(obj) or figure out what's going on your side before attempting a switch case. – Tiramonium Commented Jun 13, 2022 at 21:16
Add a ment  | 

4 Answers 4

Reset to default 1

Switch operator only works for primitives, it's not possible to pare objects, but you can create primitive by yourself, e.g string

const pareValue = val_a + ' ' + val_b;

switch (pareValue){
  case 'x y':
    //"some code here"
    break;
}

The value in a case statement must be a constant or a literal or an expression that evaluates to a constant or a literal. You can't expect a switch statement to have objects and underyling case statements have their own objects and do a parison.

Having said that you've options in javascript that allow you to transform an object (just for parison purpose). An approach I would following would be like this

let obj = {'a': 'x','b': 'y'};
let obj1={a:'x',b:'y'};
switch(Object.values(obj).join(','))
{
 case Object.values(obj1).join(','):
   console.log('evaluation succeeds');
 break;
}

So what I've done is took the object values and joined with a ma(effectively having a string), and in the case statement did same with another object (so that parison could take place)

switch(true) {
  case obj1.a == obj2.a && obj1.b == obj2.b:
    console.log('true');
    break;
}

Once you figure out whether you have a real JavaScript object or if you have to parse a JSON into a object first, your case needs to include the entire object to work.

It would look like this:

const obj = { a: "value_a", b: "value_b" };

switch (obj) {
  case { a: "value_a", b: "value_b" }:
    // insert your code here
    break;
  default:
  case { a: "value_a2", b: "value_b3" }:
    // insert your code here
    break;
}

As long as your case matches the entire object, it will work fine. Or you could make one to match a single property, which in that case would look like:

switch (obj.a) {
  case "value_a":
    // insert your code here
    break;
  default:
  case "value_a2":
    // insert your code here
    break;
}

本文标签: javascriptAngular typescript switch case by some valuesStack Overflow