新老数据对比

判断新数据与原始数据中有何不同(增删改)

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
  }
上一篇:(CoRL2020)DIRL: Domain-Invariant Representation Learning Approach for Sim-to-Real Transfer 论文笔记


下一篇:数组中的for..in../for..of../forEach()方法的区别