我的日常开发记录日志
首页
  • Laravel
  • Thinkphp
  • Swoole
  • Workman
  • php
  • HTML
  • CSS
  • JavaScript
  • Vue
  • ES6
  • 小程序
  • Mysql
  • Redis
  • Es
  • MongoDb
  • Git
  • Composer
  • Linux
  • Nginx
  • Docker
  • Vpn
  • 开发实战
  • 开发工具类
  • 友情链接
💖关于
💻收藏
  • 分类
  • 标签
  • 归档数据
GitHub (opens new window)

我的日常开发记录日志

never give up
首页
  • Laravel
  • Thinkphp
  • Swoole
  • Workman
  • php
  • HTML
  • CSS
  • JavaScript
  • Vue
  • ES6
  • 小程序
  • Mysql
  • Redis
  • Es
  • MongoDb
  • Git
  • Composer
  • Linux
  • Nginx
  • Docker
  • Vpn
  • 开发实战
  • 开发工具类
  • 友情链接
💖关于
💻收藏
  • 分类
  • 标签
  • 归档数据
GitHub (opens new window)
  • 工具类

  • 问题技巧以及优化

    • 优化技巧

      • axios发送get请求url传递字符时需要进行编码
      • 当选择某一项时,需要把指定项的值存储起来后使用
      • 优化&运算
      • 优化if语句中相同的代码案例
      • 解决v-for和v-if不能同时使用问题
      • 优化从两个数组中循环判断相等取值操作
  • 博客搭建

  • GitHub技巧

  • 开发工具

  • 开发实战

  • sd

  • 技术
  • 问题技巧以及优化
  • 优化技巧
小胖墩er
2020-09-09

优化if语句中相同的代码案例

优化前

if ([0, 1].includes(loadType)) {
  this.energyData = res.data.energyItems;
  this.energyArr = this.energyData.length > 9 ? this.energyData.slice(0, 9) : (this.energyArr = this.energyData)
  this.getEnergyRank()
}
if ([0, 2].includes(loadType)) {
  this.constData = res.data.energyItems;
  this.costArr = this.constData.length > 9 ? this.constData.slice(0, 9) : (this.costArr = this.constData)
  this.costRank()
}
1
2
3
4
5
6
7
8
9
10

优化后

function handle({thisKey,dataKey,thisArr}) {
  this[thisKey] = res.data[dataKey];
  this[thisArr] = this[thisKey].length > 9 ? this[thisKey].slice(0, 9) : (this[thisArr] = this[thisKey])
}


let key = {
  1: {
    dataKey: 'energyItems',
    thisKey: 'energyData',
    thisArr: 'energyArr'
  },
  2: {
    dataKey: 'costItems',
    thisKey: 'constData',
    thisArr: 'costArr'
  }
}

if ([0, 1].includes(loadType)) {
  this.handle(key[1])
  this.getEnergyRank()
}
if ([0, 2].includes(loadType)) {
  this.handle(key[2])
  this.costRank()
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
在线编辑 (opens new window)
#技巧总结
上次更新: 2022/10/10, 09:51:26
优化&运算
解决v-for和v-if不能同时使用问题

← 优化&运算 解决v-for和v-if不能同时使用问题→

最近更新
01
showprocess用法
04-29
02
vue3中尖括号和冒号的使用细则
04-29
03
sd使用
02-22
更多文章>
🖥️

© 2025窝窝侠 💌 豫ICP备20005263号-2 🛀 Theme by 💝 Vdoing && 小胖墩er

  • 跟随系统
  • 浅色模式
  • 深色模式
  • 阅读模式
×