随笔(四)——代码优化

文章目录


前言

原逻辑:后端data数据中返回数组,数组中有两个对象,一个是属性指标,一个是应用指标,根据这两个指标展示不同的多选框


1.原本代码

javascript 复制代码
getIndicatorRange(indexReportList, target) {
  const indexList = []
      const indexObj = {}
      indexReportList.forEach(item => {
        item.showReportFieldList.forEach(fieldItem => {
          indexObj[fieldItem.showFieldTag] = {
            checked: target.includes(fieldItem.showFieldTag),
            disabled: item.name === '属性指标'
          }

          if (target.includes(fieldItem.showFieldTag)) {
            indexList.push({
              ...fieldItem,
              // isAttri: item.name === '属性指标', // 判断是否是属性指标
              disabled: item.name === '属性指标'
            })
          }
        })
      })
  return {
        indexList,
        indexObj
      }
 },



indexReportList.forEach(item => {
        const tarList = [...target] // 浅拷贝数组
        tarList.forEach(tarItem => {
          item.showReportFieldList.forEach(fieldItem => {
            indexObj[fieldItem.showFieldTag] = {
              checked: tarItem === fieldItem.showFieldTag,
              disabled: item.name === '属性指标'
            }

            if (tarItem === fieldItem.showFieldTag) {
              indexList.push({
                ...fieldItem,
                // isAttri: item.name === '属性指标', // 判断是否是属性指标
                disabled: item.name === '属性指标'
              })
            }
          })
        })
      })

2.新增逻辑

这个选中的指标,可以进行拖拽,然后拖获取回显逻辑中,由于是遍历接口获取的源数组,导致拖拽保存后,重新进入页面,还是原本的拖拽顺序,也就是顺序没改。

3.优化逻辑

正常是在最外层使用遍历拖拽后的新数组,获取一个新的源数组进行渲染,但是这样加上本身的for循环,就有三层for循环了。优化后的代码如下:拷贝展示数据,遍历这个数据,将里面的是否存在,直接和item 的数据进行判断即可

优化后的代码

javascript 复制代码
getIndicatorRange(indexReportList, target) {

  const indexList = []
      const indexObj = {}
      const targetMap = new Map();
      // 构建 targetMap,保持 target 的顺序
      target.forEach((tarItem, index) => {
        targetMap.set(tarItem, index);
      });
      indexReportList.forEach(item => {
        item.showReportFieldList.forEach(fieldItem => {
          const showFieldTag = fieldItem.showFieldTag;
          const isAttri = item.name === '属性指标';
          // 更新 indexObj
          if (!indexObj[showFieldTag]) {
            indexObj[showFieldTag] = {
              checked: false,
              disabled: isAttri
            };
          }
          // 如果 showFieldTag 在 target 中,更新 indexObj 并插入 indexList
          if (targetMap.has(showFieldTag)) {
            indexObj[showFieldTag].checked = true;
            // 插入 indexList,保持 target 的顺序
            indexList[targetMap.get(showFieldTag)] = {
              ...fieldItem,
              disabled: isAttri
            };
          }
        })
      })
  return {
        indexList,
        indexObj
      }
},
相关推荐
爱上妖精的尾巴1 天前
8-1 WPS JS宏 String.raw等关于字符串的3种引用方式
前端·javascript·vue.js·wps·js宏·jsa
hvang19881 天前
某花顺隐藏了重仓涨幅,通过chrome插件计算基金的重仓涨幅
前端·javascript·chrome
web打印社区1 天前
vue页面打印:printjs实现与进阶方案推荐
前端·javascript·vue.js·electron·html
daols881 天前
vue2 甘特图 vxe-gantt 一行渲染多个子任务的配置
vue.js·甘特图·vxe-table
天若有情6731 天前
XiangJsonCraft v1.2.0重大更新解读:本地配置优先+全量容错,JSON解耦开发体验再升级
前端·javascript·npm·json·xiangjsoncraft
2501_944525541 天前
Flutter for OpenHarmony 个人理财管理App实战 - 预算详情页面
android·开发语言·前端·javascript·flutter·ecmascript
摘星编程1 天前
React Native + OpenHarmony:自定义useFormik表单处理
javascript·react native·react.js
pas1361 天前
39-mini-vue 实现解析 text 功能
前端·javascript·vue.js
qq_532453531 天前
使用 GaussianSplats3D 在 Vue 3 中构建交互式 3D 高斯点云查看器
前端·vue.js·3d
2601_949833391 天前
flutter_for_openharmony口腔护理app实战+我的实现
开发语言·javascript·flutter