判断新数据与原始数据中有何不同(增删改)
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
}