Compare datos antiguos y nuevos para averiguar qué se ha agregado y qué se ha eliminado

Compare los datos nuevos con los datos antiguos, descubra la adición y eliminación, y póngalos en dos matrices respectivamente

Compare dos arreglos, primero encuentre el mismo y compárelo con el arreglo original y el nuevo arreglo para realizar operaciones de deduplicación.
Cuando el arreglo es un objeto, puede seleccionar el único atributo en el objeto para compararlo.

const values = await this.form.validateFields();
const {
    
     oldData } = this.state;//旧数据
let oldDataRepetition = [].concat(oldData);    //使用空数组合并,之后操作不改变原数组
const dataSource = values.companyInfo;//新数据
let dataSourceRepetition = [].concat(dataSource);
let someArr = [];   //相同的
for (let i = 0; i < dataSource.length; i++) {
    
    
    for (let j = 0; j < oldData.length; j++) {
    
    
        if (oldData[j].companyId.value === dataSource[i].companyId.value) {
    
    
            someArr.push(oldData[j]);
        }
    }
}

let deleteArr = [];  //删除的
for (let i = 0; i < someArr.length; i++) {
    
    
    for (let j = 0; j < oldDataRepetition.length; j++) {
    
    
        if (someArr[i].companyId.value === oldDataRepetition[j].companyId.value) {
    
    
            oldDataRepetition.splice(j, 1);    //去重
        }
    }
}
deleteArr = oldDataRepetition;

let addArr = [];  //增加的
for (let i = 0; i < someArr.length; i++) {
    
    
    for (let j = 0; j < dataSourceRepetition.length; j++) {
    
    
        if (someArr[i].companyId.value === dataSourceRepetition[j].companyId.value) {
    
    
            dataSourceRepetition.splice(j, 1);    //去重
        }
    }
}
addArr = dataSourceRepetition;

Supongo que te gusta

Origin blog.csdn.net/weixin_53125679/article/details/124019305
Recomendado
Clasificación