I have two arrays, One is accounts and another one is departments.
Requirement:
-> Need to compare both array and need to get the filtered accounts array.
-> Accounts array is single level whereas departments array has two level and I am in the need to compare a value of JobTitleID both in accounts and department.
-> And finally return the modified accounts array with filtered data.
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 filteredData = accounts.map(item => {
departments.map(levelOne => {
levelOne.jobTitles.map(levelTwo => {
return levelTwo.JobTitleID === item.JobTitleID
})
})
})
console.log('filteredData ', filteredData)
Expected Solution:
[
{
"AccountId": 25205,
"CandidateID": 8474,
"JobTitleID": 250
},
{
"AccountId": 25206,
"CandidateID": 8474,
"JobTitleID": 253
},
{
"AccountId": 25237,
"CandidateID": 8474,
"JobTitleID": 255
},
{
"AccountId": 25265,
"CandidateID": 8474,
"JobTitleID": 362
}
]
Things I have tried:
const filteredData = accounts.map(item => {
departments.map(levelOne => {
levelOne.jobTitles.map(levelTwo => {
return levelTwo.JobTitleID === item.JobTitleID
})
})
})
But I believe my approach is entirely a wrong one.. Kindly please help me to achieve the expected solution by comparing two arrays.. Thanks in advance.