新老数据对比
作者:互联网
判断新数据与原始数据中有何不同(增删改)
const formatSaveList = (newList, oldList) => {
const insertList = [];
const deleteList = [];
const updateList = [];
// 找到新增和修改
newList.forEach(newItem => {
let insertFlag = true;
// 找到相同数据,判断是否发生改变,找到需要修改的
oldList.forEach(oldItem => {
if (newItem.uuid === oldItem.uuid) {
insertFlag = false;
if (JSON.stringify(newItem) !== JSON.stringify(oldItem)) {
updateList.push(newItem);
}
}
});
// 判断是否有相同Id,找到新增的数据
if (insertFlag) {
insertList.push(newItem);
}
});
// 找到删除
oldList.forEach(oldItem => {
let deleteFlag = true;
newList.forEach(newItem => {
if (oldItem.uuid === newItem.uuid) {
deleteFlag = false;
}
});
if (deleteFlag) {
deleteList.push(oldItem);
}
});
const data = {
insertList: insertList,
deleteList: deleteList,
updateList: updateList
};
return data;
};
export {
formatSaveList
}
标签:const,oldItem,deleteList,新老,updateList,newItem,数据,对比,forEach 来源: https://www.cnblogs.com/psmart/p/15774872.html