0

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!

2
  • ...what kind of equality comparision should happen in the expression above, in your opinion, i wonder ( aka, isEqual("{'a': val_a,'b': val_b}", {'x','y'})...? :/ 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. Commented Jun 13, 2022 at 21:16

4 Answers 4

1

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

const compareValue = val_a + ' ' + val_b;

switch (compareValue){
  case 'x y':
    //"some code here"
    break;
}
Sign up to request clarification or add additional context in comments.

Comments

0

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 comparison.

Having said that you've options in javascript that allow you to transform an object (just for comparison 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 comma(effectively having a string), and in the case statement did same with another object (so that comparison could take place)

Comments

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

Comments

0

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;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.