Nested switch statement in javascript

前端 未结 4 1161
礼貌的吻别
礼貌的吻别 2021-02-05 05:34

Is it possible to use nested switch statement in javascript.

My code is some what look like

switch(id1)
{
case 1:
     switch(id2){
     ca         


        
4条回答
  •  攒了一身酷
    2021-02-05 06:13

    Your approach is absolutely fine.

    You can make the switch nesting less complex by using switch (true):

    switch (true) {
    case ((id1 === 1) && (id2 === 1) && (id3 === 1)) :
    case ((id1 === 1) && (id2 === 1) && (id3 === 2)) :
    case ((id1 === 1) && (id2 === 2) && (id3 === 1)) :
    case ((id1 === 1) && (id2 === 2) && (id3 === 2)) :
    case ((id1 === 2) && (id2 === 1) && (id3 === 1)) :
    case ((id1 === 2) && (id2 === 1) && (id3 === 2)) :
    case ((id1 === 2) && (id2 === 2) && (id3 === 1)) :
    case ((id1 === 2) && (id2 === 2) && (id3 === 2)) :
    }
    

提交回复
热议问题