使用lodash根据来自两个不同数组的数据过滤数组

时间:2019-06-18 13:03:04

标签: javascript lodash

我需要根据两个不同数组的数据过滤一个数组。下面是示例和预期结果。

let groupedProtocolFilterValues = [
    ["Risk score < 4"]
];
let groupedPatientFilterValues = [
    ["CARDIOLOGY B2 INTERN2 ADMIT PRIMARY TEAM", "CARDIOLOGY CHF INTERN2 ADMIT PRIMARY TEAM"]
];

let events = [{
    "_id": "5d087b842c3f77e7a0c674e5",
    "alert": {
        "title": "Risk score < 3",
        "patientLocation": "CARDIOLOGY CHF INTERN2 ADMIT PRIMARY TEAM\r\r\nCARDIOMYOPATHY CONSULT"
    },
    "createdAt": "2019-06-18T05:49:56.402Z"
}, {
    "_id": "5d087b842c3f77e7a0c674e6",
    "alert": {
        "title": "Risk score < 4",
        "patientLocation": "CARDIOLOGY CHF INTERN2 ADMIT PRIMARY TEAM\r\r\nCARDIOMYOPATHY CONSULT"
    },
    "createdAt": "2019-06-18T05:49:56.402Z"
}, {
    "_id": "5d087b842c3f77e7a0c674e7",
    "alert": {
        "title": "Risk score < 4",
        "patientLocation": "CARDIOLOGY CHF INTERN2 ADMIT PRIMARY TEAM\r\r\nCARDIOMYOPATHY CONSULT"
    },
    "createdAt": "2019-06-18T05:49:56.402Z"
}, {
    "_id": "5d087b842c3f77e7a0c674e8",
    "alert": {
        "title": "Risk score < 4",
        "patientLocation": "CARDIOLOGY B1 INTERN1 ADMIT PRIMARY TEAM\r\r\nCARDIOMYOPATHY CONSULT"
    },
    "createdAt": "2019-06-18T05:49:56.402Z"
}];

在这里,我想根据“ groupedProtocolFilterValues”和“ groupedPatientFilterValues”的数据过滤“事件”数组。因此,查看当前数据的预期结果应该是

events = [{
    "_id": "5d087b842c3f77e7a0c674e6",
    "alert": {
        "title": "Risk score < 4",
        "patientLocation": "CARDIOLOGY CHF INTERN2 ADMIT PRIMARY TEAM\r\r\nCARDIOMYOPATHY CONSULT"
    },
    "createdAt": "2019-06-18T05:49:56.402Z"
}, {
    "_id": "5d087b842c3f77e7a0c674e7",
    "alert": {
        "title": "Risk score < 4",
        "patientLocation": "CARDIOLOGY CHF INTERN2 ADMIT PRIMARY TEAM\r\r\nCARDIOMYOPATHY CONSULT"
    },
    "createdAt": "2019-06-18T05:49:56.402Z"
}];

我想检查任何“事件”数组元素是否包含“风险得分<4”和“ CARDIOLOGY B2 INTERN2允许主要团队”或“ CARDIOLOGY CHF INTERN2允许主要团队”。

简而言之,我需要应用以下过滤条件:

与“ groupedPatientFilterValues / groupedProtocolFilterValues”之间的联系

在'groupedPatientFilterValues'和'groupedProtocolFilterValue'中使用

2 个答案:

答案 0 :(得分:1)

此任务不需要使用lodash,@Entity @Table(name="takeCourse") public class TakeCourse implements Serializable { @EmbeddedId private CompositeKey id; @MapsId @ManyToOne @JoinColumn(name = "sid") private Student student; @MapsId @ManyToOne @JoinColumn(name = "cid") private Course course; // getters and setters } 将完成工作:

.filter

答案 1 :(得分:0)

使用filter代替lodash。

let groupedProtocolFilterValues = [
  ["Risk score < 4"]
];
let groupedPatientFilterValues = [
  ["CARDIOLOGY B2 INTERN2 ADMIT PRIMARY TEAM", "CARDIOLOGY CHF INTERN2 ADMIT PRIMARY TEAM"]
];

let events = [{
  "_id": "5d087b842c3f77e7a0c674e5",
  "alert": {
    "title": "Risk score < 3",
    "patientLocation": "CARDIOLOGY CHF INTERN2 ADMIT PRIMARY TEAM\r\r\nCARDIOMYOPATHY CONSULT"
  },
  "createdAt": "2019-06-18T05:49:56.402Z"
}, {
  "_id": "5d087b842c3f77e7a0c674e6",
  "alert": {
    "title": "Risk score < 4",
    "patientLocation": "CARDIOLOGY CHF INTERN2 ADMIT PRIMARY TEAM\r\r\nCARDIOMYOPATHY CONSULT"
  },
  "createdAt": "2019-06-18T05:49:56.402Z"
}, {
  "_id": "5d087b842c3f77e7a0c674e7",
  "alert": {
    "title": "Risk score < 4",
    "patientLocation": "CARDIOLOGY CHF INTERN2 ADMIT PRIMARY TEAM\r\r\nCARDIOMYOPATHY CONSULT"
  },
  "createdAt": "2019-06-18T05:49:56.402Z"
}, {
  "_id": "5d087b842c3f77e7a0c674e8",
  "alert": {
    "title": "Risk score < 4",
    "patientLocation": "CARDIOLOGY B1 INTERN1 ADMIT PRIMARY TEAM\r\r\nCARDIOMYOPATHY CONSULT"
  },
  "createdAt": "2019-06-18T05:49:56.402Z"
}];

const output = events.filter(({
  alert: {
    title,
    patientLocation
  }
}) => {
  return groupedProtocolFilterValues[0].includes(title) && groupedPatientFilterValues[0].some((val) => {
    const patientLocationName = patientLocation.split('\n')[0];
    return val.trim().includes(patientLocationName.trim());
  });
});

console.log(output);