switch 多个case的用法


switch 中想要多个判断都进到一个分支中可以这样写

for (const item of [1, 2, 3, 4, 5, 6]) {
  switch (item) {
    case 1:
    case 2:
    case 3:
    case 4: //以上case都执行这个分支
      console.log("进到1,2,3,4里", item);
      break;
    case 5:
    case 6:
      console.log("进到5,6里", item);
      break;
    default:
      break;
  }
}
//进到1,2,3,4里 1
//进到1,2,3,4里 2
//进到1,2,3,4里 3
//进到1,2,3,4里 4
//进到5,6里 5
//进到5,6里 6


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM