55 lines
1.7 KiB
JavaScript
55 lines
1.7 KiB
JavaScript
export function compareArray(oldData,newData,column){
|
|
|
|
let compareResult = {
|
|
"delete":[],
|
|
"add":[],
|
|
"same":[],
|
|
}
|
|
|
|
let type = arguments.length;
|
|
|
|
if(type == 3){ // 如果是比较的数组元素是Object
|
|
// 获取新增的元素
|
|
for(let i=0;i<newData.length;i++){
|
|
let newObj = oldData.find((item,index)=>{
|
|
return item[column] == newData[i][column];
|
|
})
|
|
if(!newObj){
|
|
compareResult.add.push(newData[i]);
|
|
}else{
|
|
compareResult.same.push(newData[i]);
|
|
}
|
|
}
|
|
// 获取删除的元素
|
|
for(let i=0;i<oldData.length;i++){
|
|
let delObj = newData.find((item,index)=>{
|
|
return item[column] == oldData[i][column]
|
|
})
|
|
if(!delObj){
|
|
compareResult.delete.push(oldData[i]);
|
|
}
|
|
}
|
|
}else if(type == 2){ // 如果是比较的数组元素是基本类型
|
|
// 获取新增的元素
|
|
for(let i=0;i<newData.length;i++){
|
|
let newObj = oldData.find((item,index)=>{
|
|
return item == newData[i];
|
|
})
|
|
if(!newObj){
|
|
compareResult.add.push(newData[i]);
|
|
}else{
|
|
compareResult.same.push(newData[i]);
|
|
}
|
|
}
|
|
// 获取删除的元素
|
|
for(let i=0;i<oldData.length;i++){
|
|
let delObj = newData.find((item,index)=>{
|
|
return item == oldData[i]
|
|
})
|
|
if(!delObj){
|
|
compareResult.delete.push(oldData[i]);
|
|
}
|
|
}
|
|
}
|
|
return compareResult;
|
|
} |