我有两个数组,我需要使用lodash检查数组元素是否包含另一个数组元素的子字符串。下面是一个示例。
let array1 = ['CARDIOLOGY CHF INTERN2 ADMIT PRIMARY TEAM\r\r\nCARDIOMYOPATHY CONSULT',
CARDIOLOGY CHF INTERN2 ADMIT PRIMARY TEAM\r\r\PALLIATIVE CARE HEARTPAL CONSULT\r\r\PSYCHIATRY CONSULT A\r\r\nCARDIOMYOPATHY CONSULT'];
let array2 = [
'CARDIOLOGY B1 INTERN1 ADMIT PRIMARY TEAM',
'CARDIOLOGY B1 INTERN2 ADMIT PRIMARY TEAM',
'CARDIOLOGY B2 INTERN1 ADMIT PRIMARY TEAM',
'CARDIOLOGY B2 INTERN2 ADMIT PRIMARY TEAM',
'CARDIOLOGY B3 INTERN1 ADMIT PRIMARY TEAM',
'CARDIOLOGY B3 INTERN2 ADMIT PRIMARY TEAM',
'CARDIOLOGY CHF INTERN1 ADMIT PRIMARY TEAM',
'CARDIOLOGY CHF INTERN2 ADMIT PRIMARY TEAM'];
所以在这里,我要检查array2的任何元素中是否存在来自array2的元素“ CARDIOLOGY B1 INTERN1 ADMIT PRIMARY TEAM”,如果存在,那么我们要将其存储在新数组中。
目前,我已经做了类似的事情来检查一个字符串。
_.some(patientLocations, (el) => _.includes('CARDIOLOGY CHF INTERN2 ADMIT PRIMARY TEAM\r\r\nCARDIOMYOPATHY CONSULT', el));
答案 0 :(得分:1)
以下是为您提供的解决方案(不使用lodash,但是欢迎您用lodash替换常规函数-结果将是相同的):
let array1 = ['CARDIOLOGY CHF INTERN2 ADMIT PRIMARY TEAM\r\r\nCARDIOMYOPATHY CONSULT',
'CARDIOLOGY CHF INTERN2 ADMIT PRIMARY TEAM\r\r\PALLIATIVE CARE HEARTPAL CONSULT\r\r\PSYCHIATRY CONSULT A\r\r\nCARDIOMYOPATHY CONSULT'];
let array2 = [
'CARDIOLOGY B1 INTERN1 ADMIT PRIMARY TEAM',
'CARDIOLOGY B1 INTERN2 ADMIT PRIMARY TEAM',
'CARDIOLOGY B2 INTERN1 ADMIT PRIMARY TEAM',
'CARDIOLOGY B2 INTERN2 ADMIT PRIMARY TEAM',
'CARDIOLOGY B3 INTERN1 ADMIT PRIMARY TEAM',
'CARDIOLOGY B3 INTERN2 ADMIT PRIMARY TEAM',
'CARDIOLOGY CHF INTERN1 ADMIT PRIMARY TEAM',
'CARDIOLOGY CHF INTERN2 ADMIT PRIMARY TEAM'];
array2.forEach(subStr => {
const found = array1.some(array1Value => array1Value.indexOf(subStr) > -1);
if (found) {
console.log(`"${subStr}" was found in array1`)
} else {
console.log(`"${subStr}" was NOT found in array1`)
}
});
答案 1 :(得分:1)
不确定lodash,但这应该使用纯JavaScript
array2.filter(patientLocation => !!array1.find(element => element.includes(patientLocation)))