Answer:
You can do a filter of the student data that resolves any data that reports the date exists within the attendance record by using some.
let searchAbsentDate = (data, date) =>
data.filter(student =>
student.attendance.some(record =>
record.absent_on == date));
Example:
let students=[{batch_id:22,id:1,image:null,name:"a new batch student",attendance:[{id:1,student_id:1,batch_id:22,absent_on:"2019-09-15",time:"09:26:23"},{id:9,student_id:1,batch_id:22,absent_on:"2019-09-19",time:"00:00:00"}]},{batch_id:22,id:2,image:null,name:"a new batch student",attendance:[{id:9,student_id:2,batch_id:22,absent_on:"2019-09-19",time:"00:00:00"}]},{batch_id:22,id:12,image:null,name:"a new batch student",attendance:[]}];
let searchAbsentDate = (data, date) =>
data.filter(student =>
student.attendance.some(record =>
record.absent_on == date));
console.log( searchAbsentDate(students, "2019-09-19") );
Dealing with Validation:
Although the above is really all you need to do what you're asking, it lacks validation and error handling. If that's important you might consider a long-hand function like:
function searchAbsentDate(data, date) {
// helper methods
const isDateFormat = datestring => /\d{4}-\d{2}-\d{2}/.test(datestring),
isNotString = s => !typeof s === "string",
noAttendance = student => !student.attendance || student.attendance.length === 0,
invalidStudentAttendance = attendance => !Array.isArray(attendance),
noAbsentDate = absentRecord => !absentRecord;
// determine date is in correct format
if(isNotString(date) || !isDateFormat(date)) {
throw Error("Improper Date Format");
}
// determine data is array
if(!Array.isArray(data)) {
throw Error("Student Records is not Array");
}
// get results of filter
let studentsAbsentOnDate = data.filter(function studentRecord(student, index) {
// check if student has attendance records
if(noAttendance(student)) return false;
// determine if valid student record format
if(invalidStudentAttendance(student.attendance)) {
throw Error(`Invalid Student Record Format At ${index}`);
}
// determine if student has absent date
return student.attendance.some(function wasAbsentOnDate(record, _index) {
// determine if valid attendance record format
if(noAbsentDate(record.absent_on)) {
throw Error(`Invalid Attendance Record Format At Student:${index} Attendance Record:${_index}`);
}
// does attendance show absent on specified date
return record.absent_on == date;
});
});
// return any student records that match date
return studentsAbsentOnDate;
}
Example:
let students=[{batch_id:22,id:1,image:null,name:"a new batch student",attendance:[{id:1,student_id:1,batch_id:22,absent_on:"2019-09-15",time:"09:26:23"},{id:9,student_id:1,batch_id:22,absent_on:"2019-09-19",time:"00:00:00"}]},{batch_id:22,id:2,image:null,name:"a new batch student",attendance:[{id:9,student_id:2,batch_id:22,absent_on:"2019-09-19",time:"00:00:00"}]},{batch_id:22,id:12,image:null,name:"a new batch student",attendance:[]}];
function searchAbsentDate( data, date ) {
const isDateFormat = datestring => /\d{4}-\d{2}-\d{2}/.test( datestring ),
isNotString = s => !typeof s === "string",
noAttendance = student => !student.attendance || student.attendance.length === 0,
invalidStudentAttendance = attendance => !Array.isArray( attendance ),
noAbsentDate = absentRecord => !absentRecord;
if ( isNotString( date ) || !isDateFormat( date ) ) {
throw Error( "Improper Date Format" );
}
if ( !Array.isArray( data ) ) {
throw Error( "Student Records is not Array" );
}
let studentsAbsentOnDate = data.filter( function studentRecord( student, index ) {
if ( noAttendance( student ) ) return false;
if ( invalidStudentAttendance( student.attendance ) ) {
throw Error( `Invalid Student Record Format At ${index}` );
}
return student.attendance.some( function wasAbsentOnDate( record, _index ) {
if ( noAbsentDate( record.absent_on ) ) {
throw Error( `Invalid Attendance Record Format At Student:${index} Attendance Record:${_index}` );
}
return record.absent_on == date;
} );
} );
return studentsAbsentOnDate;
}
console.log( searchAbsentDate(students, "2019-09-19") );
Alternate Code Style:
Alternatively to the above, and my personal preference, you can abstract the parts away( checking, error handling, transformations ) into additional helper methods.
This gives you the added flexibility in storing these parts separately for cleanliness/reuse if you'd like, but the goal is to streamline the execution of the function so that it's both readable and easy to maintain/change in the future.
I find this style to be particularly useful when dealing with Projects involving Vue, React, and other state/rendering focused libraries and frameworks.
function searchAbsentDate( students, date ) {
/*
HELPER METHODS
*/
const
// FORMAT CHECK-> date
isDateFormat = datestring => /\d{4}-\d{2}-\d{2}/.test( datestring ),
isNotString = s => !typeof s === "string",
isNotDate = potentialDate => ( isNotString( potentialDate ) || !isDateFormat ),
// FORMAT CHECK-> student
noAttendanceRecords = student => !student.attendance || student.attendance.length === 0,
invalidStudentAttendanceRecords = attendance => !Array.isArray( attendance ),
// FORMAT CHECK-> attendance record
noAbsentDate = absentDate => !absentDate,
// DATA TRANSFORMS
forAllAttendanceRecords = ( attendanceRecords, index ) => checkFn => attendanceRecords.some( checkFn( index ) ),
filterRecords = students => filterFn => students.filter( filterFn ),
// FILTERS
ifAbsentOnDate = date => index => ( record, _index ) => {
if ( noAbsentDate( record.absent_on ) ) err.throwMissingDate( index, _index );
return record.absent_on == date;
},
forAbsenceOnDate = ( student, studentIndex ) => {
if ( noAttendanceRecords( student ) ) return false;
if ( invalidStudentAttendanceRecords( student.attendance ) ) err.throwInvalidStudentRecord();
return forAllAttendanceRecords( student.attendance, studentIndex )( ifAbsentOnDate( date ) );
},
// ERROR HANDLERS
err = {
throwMissingDate( stu_i, ar_i ) {
throw Error( "Invalid Attendance Record Format At Student:" + stu_i + "Attendance Record:" + ar_i );
},
throwInvalidStudentRecord( index ) {
throw Error( "Invalid Student Record Format At " + index );
},
throwStudentRecordsFormat() {
throw Error( "Student Records is not Array" );
},
throwDateFormat() {
throw Error( "Improper Date Format" );
}
};
/*
EXECUTION
*/
if ( isNotDate( date ) ) err.throwDateFormat();
if ( !Array.isArray( students ) ) err.throwStudentRecordsFormat();
return filterRecords( students )( forAbsenceOnDate );
}
Example:
let students = [{ batch_id: 22, id: 1, image: null, name: "a new batch student", attendance: [{ id: 1, student_id: 1, batch_id: 22, absent_on: "2019-09-15", time: "09:26:23" }, { id: 9, student_id: 1, batch_id: 22, absent_on: "2019-09-19", time: "00:00:00" }] }, { batch_id: 22, id: 2, image: null, name: "a new batch student", attendance: [{ id: 9, student_id: 2, batch_id: 22, absent_on: "2019-09-19", time: "00:00:00" }] }, { batch_id: 22, id: 12, image: null, name: "a new batch student", attendance: [] }];
function searchAbsentDate( students, date ) {
const
isDateFormat = datestring => /\d{4}-\d{2}-\d{2}/.test( datestring ),
isNotString = s => !typeof s === "string",
isNotDate = potentialDate => ( isNotString( potentialDate ) || !isDateFormat ),
noAttendanceRecords = student => !student.attendance || student.attendance.length === 0,
invalidStudentAttendanceRecords = attendance => !Array.isArray( attendance ),
noAbsentDate = absentDate => !absentDate,
forAllAttendanceRecords = ( attendanceRecords, index ) => checkFn => attendanceRecords.some( checkFn( index ) ),
filterRecords = students => filterFn => students.filter( filterFn ),
ifAbsentOnDate = date => index => ( record, _index ) => {
if ( noAbsentDate( record.absent_on ) ) err.throwMissingDate( index, _index );
return record.absent_on == date;
},
forAbsenceOnDate = ( student, studentIndex ) => {
if ( noAttendanceRecords( student ) ) return false;
if ( invalidStudentAttendanceRecords( student.attendance ) ) err.throwInvalidStudentRecord();
return forAllAttendanceRecords( student.attendance, studentIndex )( ifAbsentOnDate( date ) );
},
err = {
throwMissingDate( stu_i, ar_i ) {
throw Error( "Invalid Attendance Record Format At Student:" + stu_i + "Attendance Record:" + ar_i );
},
throwInvalidStudentRecord( index ) {
throw Error( "Invalid Student Record Format At " + index );
},
throwStudentRecordsFormat() {
throw Error( "Student Records is not Array" );
},
throwDateFormat() {
throw Error( "Improper Date Format" );
}
};
if ( isNotDate( date ) ) err.throwDateFormat();
if ( !Array.isArray( students ) ) err.throwStudentRecordsFormat();
return filterRecords( students )( forAbsenceOnDate );
}
console.log( searchAbsentDate( students, "2019-09-19" ) );
Whew...
I know, I know. I wrote quite a lengthy answer to this simple question - I tend to get carried away! In any case, whether or not all of this was useful to you, I hope I was able to help at least a little bit!
Good luck!
students.filter(({attendance}) => attendance.filter(day => day.absent_on === date))Should return you list of students absent ondate