Json Switch Statement?

I am wondering how to make a switch statement in json?

{"Errors":{"key1":"afkafk"},"IsValid":false,"SuccessMessage":""} 

I tried

 switch(response) { case response.Errors.key1: alert('test'); default: } 

But he seems to be ignoring my first case.

Edit

 // if undefined then go to next if statement - I am not sure if I can do something // like !=== null if (response.Errors.key1) { // display value of key1 } else if(response.Errors.Key2) { // display value of key2 differently } 

So this is what I am trying to do only with the switch statement.

+4
source share
3 answers

This will be the correct syntax:

 switch(response.Errors.key1) { case 'afkafk': alert('test'); break; default: alert('default'); } 

But I suspect that in your case the following structure is more adapted:

 { Errors: { key: 'key1', message: 'afkafk' }, IsValid: false, SuccessMessage: '' } 

because it allows you to enable the key:

 switch(response.Errors.key) { case 'key1': alert(response.Errors.message); break; default: alert('default'); } 
+8
source

It looks like you want to include the value of key1 instead of the name key1 .

 switch (response.Errors.key1) { case 'afkafk': ... } 
+2
source

I'm not quite sure what you are trying to achieve. Are you trying to switch based on the value of key1? Switch statements should be able to map the variable that you pass to the switch statement to the value of the case statement, so the following will work, although I'm not sure if this is what you are after:

 switch (response.Errors.key1) { case 'afkafk': //do something break; } 
+1
source

Source: https://habr.com/ru/post/1342052/


All Articles