Compare old and new data to find out what has been added and what has been deleted

Compare the new data with the old data, find out the addition and deletion, and put them in two arrays respectively

Compare two arrays, first find the same one, and compare it with the original array and the new array to perform deduplication operations.
When the array is an object, you can select the only attribute in the object for comparison

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;

Guess you like

Origin blog.csdn.net/weixin_53125679/article/details/124019305