2 回答

TA貢獻(xiàn)1936條經(jīng)驗 獲得超7個贊
您可以創(chuàng)建數(shù)組中所有作業(yè)的數(shù)組departments
,然后accounts
使用過濾數(shù)組.some(..)
來檢查先前生成的數(shù)組中是否存在特定作業(yè)。這是一個例子:
const accounts = [
{
"AccountId": 25205,
"CandidateID": 8474,
"JobTitleID": 250
},
{
"AccountId": 25206,
"CandidateID": 8474,
"JobTitleID": 253
},
{
"AccountId": 25232,
"CandidateID": 8474,
"JobTitleID": 257
},
{
"AccountId": 25233,
"CandidateID": 8474,
"JobTitleID": 261
},
{
"AccountId": 25236,
"CandidateID": 8474,
"JobTitleID": 256
},
{
"AccountId": 25237,
"CandidateID": 8474,
"JobTitleID": 255
},
{
"AccountId": 25245,
"CandidateID": 8474,
"JobTitleID": 281
},
{
"AccountId": 25246,
"CandidateID": 8474,
"JobTitleID": 279
},
{
"AccountId": 25265,
"CandidateID": 8474,
"JobTitleID": 362
}
]
const departments = [
{
"deptName": "Production",
"jobTitles": [
{
"JobTitleID": 246,
"DepartmentID": 72,
"JobName": "Department Foreman"
},
{
"JobTitleID": 357,
"DepartmentID": 72,
"JobName": "Maintenance Supervisor"
},
{
"JobTitleID": 247,
"DepartmentID": 72,
"JobName": "Production Manager"
},
{
"JobTitleID": 362,
"DepartmentID": 72,
"JobName": "Safety & Compliance Supervisor"
}
]
},
{
"deptName": "Engineering",
"jobTitles": [
{
"JobTitleID": 250,
"DepartmentID": 73,
"JobName": "Architect"
},
{
"JobTitleID": 248,
"DepartmentID": 73,
"JobName": "CAD Operator"
},
{
"JobTitleID": 249,
"DepartmentID": 73,
"JobName": "Engineering Manager"
},
{
"JobTitleID": 251,
"DepartmentID": 73,
"JobName": "Professional Engineer"
}
]
},
{
"deptName": "Purchasing",
"jobTitles": [
{
"JobTitleID": 253,
"DepartmentID": 74,
"JobName": "Purchasing Agent"
},
{
"JobTitleID": 255,
"DepartmentID": 74,
"JobName": "Purchasing Manager"
},
{
"JobTitleID": 252,
"DepartmentID": 74,
"JobName": "Yard Foreman"
}
]
}
]
const jobs = departments.reduce((a, c) => (a.concat(c.jobTitles)), []);
const results = accounts.filter(account => jobs.some(job => job.JobTitleID === account.JobTitleID));
console.log(results);

TA貢獻(xiàn)1810條經(jīng)驗 獲得超5個贊
您可以使用 獲得優(yōu)化解決方案Set object。Set 對象僅存儲任何類型的唯一值。首先,使用數(shù)組中的 set 對象獲取所有唯一的 JobTitleID departments。然后使用Array.prototype.filter()方法對accounts數(shù)組進(jìn)行過濾即可得到結(jié)果。
const accounts = [
{
AccountId: 25205,
CandidateID: 8474,
JobTitleID: 250,
},
{
AccountId: 25206,
CandidateID: 8474,
JobTitleID: 253,
},
{
AccountId: 25232,
CandidateID: 8474,
JobTitleID: 257,
},
AccountId: 25233,
CandidateID: 8474,
JobTitleID: 261,
},
{
AccountId: 25236,
CandidateID: 8474,
JobTitleID: 256,
},
{
AccountId: 25237,
CandidateID: 8474,
JobTitleID: 255,
},
{
AccountId: 25245,
CandidateID: 8474,
JobTitleID: 281,
},
{
AccountId: 25246,
CandidateID: 8474,
JobTitleID: 279,
},
{
AccountId: 25265,
CandidateID: 8474,
JobTitleID: 362,
},
];
const departments = [
{
deptName: 'Production',
jobTitles: [
{
JobTitleID: 246,
DepartmentID: 72,
JobName: 'Department Foreman',
},
{
JobTitleID: 357,
DepartmentID: 72,
JobName: 'Maintenance Supervisor',
},
{
JobTitleID: 247,
DepartmentID: 72,
JobName: 'Production Manager',
},
{
JobTitleID: 362,
DepartmentID: 72,
JobName: 'Safety & Compliance Supervisor',
},
],
},
{
deptName: 'Engineering',
jobTitles: [
{
JobTitleID: 250,
DepartmentID: 73,
JobName: 'Architect',
},
{
JobTitleID: 248,
DepartmentID: 73,
JobName: 'CAD Operator',
},
{
JobTitleID: 249,
DepartmentID: 73,
JobName: 'Engineering Manager',
},
{
JobTitleID: 251,
DepartmentID: 73,
JobName: 'Professional Engineer',
},
],
},
{
deptName: 'Purchasing',
jobTitles: [
{
JobTitleID: 253,
DepartmentID: 74,
JobName: 'Purchasing Agent',
},
{
JobTitleID: 255,
DepartmentID: 74,
JobName: 'Purchasing Manager',
},
{
JobTitleID: 252,
DepartmentID: 74,
JobName: 'Yard Foreman',
},
],
},
];
const set = new Set();
departments.forEach((x) => x.jobTitles.forEach((y) => set.add(y.JobTitleID)));
const ret = accounts.filter((x) => set.has(x.JobTitleID));
console.log(ret);
添加回答
舉報