当前位置: 首页 > news >正文

优化过多if else判断代码

有些判断难免会遇到很多 if else  看起来很头疼  下面是一个优化示例

修改前 

 if(this.$route.query.deptId){this.queryParams.deptId = this.$route.query.deptId}else if (this.$store.state.adaptation.roleSign.includes('fengongsicengji')) {const ancestorsId = this.$store.getters.dept.ancestors.split(',')if (ancestorsId.length === 3 || ancestorsId.length === 4) {this.queryParams.deptId = ancestorsId[2]} else if (ancestorsId.length === 2) {this.queryParams.deptId = ancestorsId[1]} else {this.queryParams.deptId = ancestorsId[2]}} 

修改后 

this.queryParams.deptId = this.$route.query.deptId || (this.$store.state.adaptation.roleSign.includes('fengongsicengji') ? (this.$store.getters.dept.ancestors.split(',').length === 3 ? ancestorsId[2] : ancestorsId[1]) : ancestorsId[2]);

日常中我们会忽略|| 的另一个用法 

if(1||2)  这是 1或者2   通过判断

const b =?(未知值)

const a = b|| 0    当b为不为false  a = b  如果为false  a = 0

优雅 太优雅了 优化完整个人还是虎躯一震的 🤧🤧🤧

http://www.lryc.cn/news/123635.html

相关文章:

  • 最强自动化测试框架Playwright (27)-跟踪查看器
  • 【工作中问题解决实践 十一】Kafka消费者消费堆积且频繁rebalance
  • ChatGpt提示词大全
  • 利用SimpleDateFormat或者LocalDateTime生成格式为“yyyy-MM-dd HH:mm:ss“的当前时间
  • 使用 Postman 批量发送请求的最佳实践
  • Docker一键部署项目,无需登录XShell
  • GIt Squash 多个提交压缩提交
  • 【数据结构】栈与队列
  • 突然让做性能测试?试试RunnerGo
  • (7)(7.4) 集结航点
  • 基于kubeadm部署K8S集群:上篇
  • 机器学习-特征选择:如何使用递归特征消除算法自动筛选出最优特征?
  • 学生成绩管理系统V1.0
  • 嵌入式:ARM Day1
  • Android 网络协议与网络编程
  • 【讯飞星火认知大模型】大模型之星火手机助理
  • centos中的swap.img可以删除吗
  • Java多线程编程中的线程死锁
  • 在浏览器中使用javascript打印HTML中指定Div带背景图片内容生成PDF电子证书查询的解决方案
  • 【Redis实践篇】使用Redisson 优雅实现项目实践过程中的5种场景
  • 污水处理厂人员定位方案介绍
  • 约数个数(质因子分解)
  • 【QT】 QSS样式表设计一文了解
  • 9-AJAX-1入门
  • ssh免密登录
  • 全球公链周进展-2023/8/14
  • python装饰器详解,python装饰器笔记心得
  • 【C语言】每日一题(寻找数组的中心下标)
  • centos 安装 nginx配置ssl 和 获取用户真实ip
  • RocketMQ 消息消费 轮询机制 PullRequestHoldService