1

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.

0

2 Answers 2

3

You can create an array of all the jobs in the departments array and then filter the accounts array using .some(..) to check if the specific job exist in the previously generated array. Here is an example:

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);

Sign up to request clarification or add additional context in comments.

Comments

2

You could get an optimize solution using Set object. Set object stores only unique values of any type. First, get all the unique JobTitleID using set object from departments array. Then use Array.prototype.filter() method to filter the accounts array to get the result.

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.