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

随笔(四)——代码优化

文章目录

  • 前言
    • 1.原本代码
    • 2.新增逻辑
    • 3.优化逻辑


前言

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


1.原本代码

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 的数据进行判断即可

在这里插入图片描述

优化后的代码

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 === '属性指标';// 更新 indexObjif (!indexObj[showFieldTag]) {indexObj[showFieldTag] = {checked: false,disabled: isAttri};}// 如果 showFieldTag 在 target 中,更新 indexObj 并插入 indexListif (targetMap.has(showFieldTag)) {indexObj[showFieldTag].checked = true;// 插入 indexList,保持 target 的顺序indexList[targetMap.get(showFieldTag)] = {...fieldItem,disabled: isAttri};}})})return {indexList,indexObj}
},
http://www.lryc.cn/news/453620.html

相关文章:

  • 安装管理K8S的开源项目KubeClipper介绍
  • 北交大研究突破:塑料光纤赋能低成本无摄像头AR/VR眼动追踪技术
  • 算法题总结(七)——哈希表
  • PS批量执行动作,ps批量修改图片大小,并修改文件的类型
  • CentOS 替换 yum源 经验分享
  • Elasticsearch基础_2.数据类型
  • docker快速安装ELK
  • GS-SLAM论文阅读笔记-CaRtGS
  • 15分钟学 Python 第36天 :Python 爬虫入门(二)
  • Spring:强制登陆与拦截器
  • MySQL-数据库约束
  • 线性表三——队列queue
  • 算法笔记(十)——队列+宽搜
  • webpack配置全面讲解【完整篇】
  • 十、kotlin的协程
  • vscode qt 最新开发环境配置, 基于最新插件 Qt All Extensions Pack
  • 【MySQL】Ubuntu环境下MySQL的安装与卸载
  • C# StringBuilder类:高效构建和修改字符串的利器
  • AVL平衡树(AVL Tree)
  • 【python实操】python小程序之两数取大值以及login登录
  • Pikachu-File Inclusion-远程文件包含
  • TIM(Timer)定时器的原理
  • Microsoft Visual Studio有多油饼
  • Golang | Leetcode Golang题解之第452题用最少数量的箭引爆气球
  • Python 从入门到实战35(进程-multiprocessing模块)
  • “米哈游悄然布局未来科技:入股星海图,共绘具身智能机器人新篇章“
  • 基于spring boot的篮球论坛系统
  • 华夏ERP账号密码泄露漏洞
  • Android问题笔记五十:构建错误-AAPT2 aapt2-7.0.2-7396180-windows Daemon
  • CAD 3dsmax maya等autodesk系列专用卸载修复工具AutoRemove,一键完全彻底卸载删除软件的专用卸载工具