const individuals = [{
"PersonId": 1,
"Name": "Person1",
"Gender": "Male",
"Courses": [
{
"StudentId": 1,
"CourseName": "Java",
"CourseCategory": "Engineering",
"CourseStatus": "Full Time"
},
{
"StudentId": 1,
"CourseName": "HR Management 2",
"CourseCategory": "HR",
"CourseStatus": "Part Time"
},
{
"StudentId": 1,
"CourseName": "Accounting 1",
"CourseCategory": "Finance",
"CourseStatus": "Full Time"
}
]
},
{
"PersonId": 2,
"Name": "Person2",
"Gender": "Female",
"Courses": [
{
"StudentId": 2,
"CourseName": "HR Management 1",
"CourseCategory": "HR",
"CourseStatus": "Part Time"
},
{
"StudentId": 2,
"CourseName": "Accounting 3",
"CourseCategory": "Finance",
"CourseStatus": "Full Time"
}
]
},
{
"PersonId": 3,
"Name": "Person3",
"Gender": "Female",
"Courses": [
{
"StudentId": 3,
"CourseName": "Java 3",
"CourseCategory": "Engineering",
"CourseStatus": "Full Time"
}
]
},
{
"PersonId": 4,
"Name": "Person4",
"Gender": "Male",
"Courses": [
{
"StudentId": 4,
"CourseName": "Java 2",
"CourseCategory": "Engineering",
"CourseStatus": "Full Time"
},
{
"StudentId": 4,
"CourseName": "Accounting 2",
"CourseCategory": "Finance",
"CourseStatus": "Part Time"
}
]
},
{
"PersonId": 5,
"Name": "Person5",
"Gender": "Male",
"Courses": [
{
"StudentId": 5,
"CourseName": "JavaScript",
"CourseCategory": "Engineering",
"CourseStatus": "Part Time"
},
{
"StudentId": 5,
"CourseName": "HR Management 5",
"CourseCategory": "HR",
"CourseStatus": "Full Time"
}
]
}];
const filterSelectedStudents = (students, gender, category, status) => {
const filteredStudents = students.filter(student => {
// compare students to selected gender
if (gender && student.gender !== gender) {
return false;
}
// check if a student has the selected category
if (category) {
const hasCategory = student.Courses.find(course => course.CourseCategory === category);
if (!hasCategory) {
return false;
}
}
// check if a student has the selected status
if (status) {
const hasStatus = student.Courses.find(course => course.CourseStatus === status);
if (!hasStatus) {
return false;
}
}
return true;
});
return filteredStudents;
};
const chosenStudents = filterSelectedStudents(individuals, null, 'HR', 'Part Time');
chosenStudents.forEach(student => {
console.log(student);
})