比较el-table里面的数据的是否有变化,深拷贝

129 阅读2分钟

比较el-table里面的数据较之前是否变化

第一步赋值

  • 从接口里获取el-table的数据,如果数据不经过处理,那么B数据变化,A数据也会变化,现在就是要A数据等于原始获取的数据,B数据是经过一系列变化后的数据;
       getDeatailList(pmtId,string){
            selectPreventiveMaintainTaskDetialsById({pmtId:pmtId}).then(res=>{
              if(string =='check'){ 
                this.originalGroupRowDetailList = res.rows  
                this.groupRowDetailList = JSON.parse(JSON.stringify( this.originalGroupRowDetailList));
              }else if(string =='protect'){
                this.firstprotectRowDetailList = res.rows  
                this.protectRowDetailList = JSON.parse(JSON.stringify(this.firstprotectRowDetailList));
              } 
            })
         },

  • 解析 this.groupRowDetailList = JSON.parse(JSON.stringify( this.originalGroupRowDetailList));段代码深拷贝的用法;
    • 这段代码的作用是对this.originalGroupRowDetailList进行深拷贝,然后将其赋值;
    • JSON.stringify()方法,会将js对象转换为JSON字符串;
    const obj = { name: "John", age: 30 };
    const jsonString = JSON.stringify(obj);
    console.log(jsonString); // 输出: '{"name":"John","age":30}' 
    
    • JSON.parse() 方法会将 JSON 字符串解析为 JavaScript 对象。
    const jsonString = '{"name":"John","age":30}';
    const obj = JSON.parse(jsonString);
    console.log(obj); // 输出: { name: "John", age: 30 } 
    
    • 结合使用
     this.groupRowDetailList =    JSON.parse(JSON.stringify(this.originalGroupRowDetailList));
    
    
  • JSON.stringify(this.originalGroupRowDetailList) :首先将 this.originalGroupRowDetailList 对象转换为一个 JSON 字符串。
  • JSON.parse(...) :接着将该字符串解析回 JavaScript 对象。

深拷贝

  • 深拷贝的含义:  对象中的每一个嵌套对象都会被复制,而不是仅仅复制对象的引用。这意味着 this.originalGroupRowDetailList 和 this.groupRowDetailList 会是两个独立的对象,修改其中一个不会影响另一个。

为什么使用这种方式

  • 目的:  在 JavaScript 中,对象是通过引用传递的,因此直接赋值会导致两个变量指向同一个对象。如果修改其中一个变量的内容,另一个变量也会受到影响。
  • JSON.parse(JSON.stringify(...)) 方法的优点:  该方法创建了一个新对象,并且其中的嵌套对象也被递归地拷贝过来,从而确保了两个对象之间没有任何引用关系。
浅拷贝时,如果修改了B对象,A对象里面的内容也会改变。
我们上面用的是深拷贝,所以新对象的内容变化后,并不会影响原来的数据,这样才会方便对比,新旧两个对象里面变化的内容。

变化值对比

  • 先看代码
  // 获取修改过的数据
        getChangedItems() {  
          let changed = [];
          let unchangedCount = 0; 
          // 遍历B数组,检查每一条数据是否发生了变化
          this.protectRowDetailList.forEach((itemB, index) => {
          const itemA = this.firstprotectRowDetailList[index];  // 获取A数组中对应的元素
          const hasChanged = itemB.implementationDate !== itemA.implementationDate || 
                            itemB.implementationContent !== itemA.implementationContent;

            if (hasChanged) {
              // 如果有变化,把修改后的数据加入到changed数组
              changed.push({ ...itemB });
            } else {
                // 判断如果没有变化且两者都不为null,则不计入unchangedCount
                if (itemB.implementationDate !== null && itemA.implementationDate !== null &&
                    itemB.implementationDate === itemA.implementationDate &&
                    itemB.implementationContent !== null && itemA.implementationContent !== null &&
                    itemB.implementationContent === itemA.implementationContent) {
                  // 如果都不为null并且内容相等,不增加 unchangedCount
                  return;
                } 
                unchangedCount++;
            }
          }); 
          // 更新到data中
          this.changedData = changed;
          this.protectunMaintainNum = unchangedCount; 
            },  
  • 对比 this.protectRowDetailListthis.firstprotectRowDetailList里面的变化;
  • forEach遍历this.protectRowDetailList,然后获取与this.firstprotectRowDetailList对应的值,对比是否相等;
  • B执行遍历的时候,顺便把A也遍历了;
this.B.forEach((itemB,index)=>{
const itemA = this.A[index]
//对比里面变化的内容,如果两个值不相等就是true
const hansChanged = itemB.C !== itemA.C
if(hansChanged){
changed.push({...itemB})
}else{
if(itemB.C !== null && itemA.C !== null &&
                    itemB.C === itemA.C ){
                    return}
  unchangedCount++;
}

)
  • 这样最终就会筛选出来,那条变更数据。并且已经有数据,且数据没变化的不会被计数到未维护的数量;