如何从两个不同的json对象中提取特定数据

n3schb8v  于 2023-05-30  发布在  其他
关注(0)|答案(1)|浏览(161)

我正在尝试提取departments数组中存在但dep_log中不存在的所有部门。我还想提取所有具有delete=1的部门。
注意:如果两个部门的id相同,并且deleted=1,我只想检索最新的一个,而不重复。
我尝试了这个方法,但得到了错误的结果:

var departments = [{
  "department_id": "1",
  "name": "sales"
}, {
  "department_id": "2",
  "name": "marketing"
}, {
  "department_id": "3",
  "name": "operation"
}];

var dep_logs = [{
  "department_id": "2",
  "creation_date": "2023-05-22 15:47:58",
  "created_by": "1",
  "deleted": "0"
}, {
  "department_id": "2",
  "creation_date": "2023-05-22 15:46:56",
  "created_by": "1",
  "deleted": "1"
}, {
  "department_id": "3",
  "creation_date": "2023-05-13 17:59:27",
  "created_by": "1",
  "deleted": "0"
}, {
  "department_id": "2",
  "creation_date": "2023-05-13 17:59:11",
  "created_by": "1",
  "deleted": "1"
}, {
  "department_id": "1",
  "creation_date": "2023-05-13 09:46:25",
  "created_by": "1",
  "deleted": "0"
}];

var finalList = []
var finalMapDeletedDepartment = []
var idSet = new Set();

dep_logs.forEach(item => {
  idSet.add(item.department_id);

  if (item.deleted === 1) {
    if (finalMapDeletedDepartment.has(item.department_id)) {
      var itemValue = finalMapDeletedDepartment.get(item.department_id);
      if (itemValue.creation_date < item.creation_date) {
        finalMapDeletedDepartment.set(item.department_id, item);
      }
    } else {
      finalMapDeletedDepartment.set(item.department_id, item);
    }
  }
});

departments.forEach(item => {
  if (!idSet.has(item.id)) {
    finalList.push(item)
  }
});
console.log(idSet);

console.log(finalMapDeletedDepartment.values());

console.log(finalList);

结果应存储在新数组中。

lnvxswe2

lnvxswe21#

我认为这段代码可以完成这项工作:

获取未登录部门:

function get_not_logged_deps() {
  // first get a list of id of logged departments
  const logged_deps_ids = dep_logs.map((l) => l.department_id);

  // then filter only departments whose id does not exists in the list
  return departments.filter((d) =>   !logged_deps_ids.includes(d.department_id));
}

获取已删除的部门(不重复的和最近的部门):

function get_deleted_deps() {
  const deleted_deps_ids = []; 

  // first sort by date desc
  // then in case of duplication only push the first one (created last) and ignore the next ones (older logs)
  return dep_logs
      .sort((a, b) => (a.creation_date < b.creation_date ? 1 : -1))
      .filter((l) => {
            if (
                l.deleted === "1" &&
                deleted_deps_ids.indexOf(l.department_id) === -1
             ) {
                deleted_deps_ids.push(l.department_id);
                return true;
               }
      });
}
var departments = [
  {
    department_id: "1",
    name: "sales",
  },
  {
    department_id: "2",
    name: "marketing",
  },
  {
    department_id: "3",
    name: "operation",
  },
];

var dep_logs = [
  {
    department_id: "2",
    creation_date: "2023-05-22 15:47:58",
    created_by: "1",
    deleted: "0",
  },
  {
    department_id: "3",
    creation_date: "2023-05-13 17:59:27",
    created_by: "1",
    deleted: "0",
  },
  {
    department_id: "2",
    creation_date: "2023-05-13 17:59:11",
    created_by: "1",
    deleted: "1",
  },
  {
    department_id: "3",
    creation_date: "2023-05-13 09:46:25",
    created_by: "1",
    deleted: "0",
  },
  {
    department_id: "2",
    creation_date: "2023-05-22 15:46:56",
    created_by: "1",
    deleted: "1",
  },
];

function get_not_logged_deps() {
  const logged_deps_ids = dep_logs.map((l) => l.department_id);

  return departments.filter((d) => !logged_deps_ids.includes(d.department_id));
}

function get_deleted_deps() {
  const deleted_deps_ids = [];

  return dep_logs
    .sort((a, b) => (a.creation_date < b.creation_date ? 1 : -1))
    .filter((l) => {
      if (
        l.deleted === "1" &&
        deleted_deps_ids.indexOf(l.department_id) === -1
      ) {
        deleted_deps_ids.push(l.department_id);
        return true;
      }
    });
}

console.log(get_not_logged_deps());
console.log(get_deleted_deps());

相关问题