随笔(四)——代码优化

[复制链接]
发表于 2026-1-13 07:24:35 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

媒介

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

1.原来代码

  1. getIndicatorRange(indexReportList, target) {
  2.   const indexList = []
  3.       const indexObj = {}
  4.       indexReportList.forEach(item => {
  5.         item.showReportFieldList.forEach(fieldItem => {
  6.           indexObj[fieldItem.showFieldTag] = {
  7.             checked: target.includes(fieldItem.showFieldTag),
  8.             disabled: item.name === '属性指标'
  9.           }
  10.           if (target.includes(fieldItem.showFieldTag)) {
  11.             indexList.push({
  12.               ...fieldItem,
  13.               // isAttri: item.name === '属性指标', // 判断是否是属性指标
  14.               disabled: item.name === '属性指标'
  15.             })
  16.           }
  17.         })
  18.       })
  19.   return {
  20.         indexList,
  21.         indexObj
  22.       }
  23. },
  24. indexReportList.forEach(item => {
  25.         const tarList = [...target] // 浅拷贝数组
  26.         tarList.forEach(tarItem => {
  27.           item.showReportFieldList.forEach(fieldItem => {
  28.             indexObj[fieldItem.showFieldTag] = {
  29.               checked: tarItem === fieldItem.showFieldTag,
  30.               disabled: item.name === '属性指标'
  31.             }
  32.             if (tarItem === fieldItem.showFieldTag) {
  33.               indexList.push({
  34.                 ...fieldItem,
  35.                 // isAttri: item.name === '属性指标', // 判断是否是属性指标
  36.                 disabled: item.name === '属性指标'
  37.               })
  38.             }
  39.           })
  40.         })
  41.       })
复制代码
2.新增逻辑

   这个选中的指标,可以举行拖拽,然后拖获取回显逻辑中,由于是遍历接口获取的源数组,导致拖拽生存后,重新进入页面,照旧原来的拖拽序次,也就是序次没改。
  3.优化逻辑

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

   优化后的代码
  1. getIndicatorRange(indexReportList, target) {
  2.   const indexList = []
  3.       const indexObj = {}
  4.       const targetMap = new Map();
  5.       // 构建 targetMap,保持 target 的顺序
  6.       target.forEach((tarItem, index) => {
  7.         targetMap.set(tarItem, index);
  8.       });
  9.       indexReportList.forEach(item => {
  10.         item.showReportFieldList.forEach(fieldItem => {
  11.           const showFieldTag = fieldItem.showFieldTag;
  12.           const isAttri = item.name === '属性指标';
  13.           // 更新 indexObj
  14.           if (!indexObj[showFieldTag]) {
  15.             indexObj[showFieldTag] = {
  16.               checked: false,
  17.               disabled: isAttri
  18.             };
  19.           }
  20.           // 如果 showFieldTag 在 target 中,更新 indexObj 并插入 indexList
  21.           if (targetMap.has(showFieldTag)) {
  22.             indexObj[showFieldTag].checked = true;
  23.             // 插入 indexList,保持 target 的顺序
  24.             indexList[targetMap.get(showFieldTag)] = {
  25.               ...fieldItem,
  26.               disabled: isAttri
  27.             };
  28.           }
  29.         })
  30.       })
  31.   return {
  32.         indexList,
  33.         indexObj
  34.       }
  35. },
复制代码
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表