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 withJSON.parse(obj)
or figure out what's going on your side before attempting a switch case. – Tiramonium Commented Jun 13, 2022 at 21:16
4 Answers
Reset to default 1Switch 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
版权声明:本文标题:javascript - Angular typescript switch case by some values - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745055872a2639930.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论