Ответ:
Вы можете сделать filter
данных ученика, которые разрешают любые данные, которые сообщают, что дата существует в записи о посещаемости, используя some
.
let searchAbsentDate = (data, date) =>
data.filter(student =>
student.attendance.some(record =>
record.absent_on == date));
Пример:
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") );
Работа с проверкой:
Хотя вышесказанное - это действительно все, что вам нужно для выполнения того, что вы просите, в нем нет проверки и обработки ошибок.,Если это важно, вы можете рассмотреть функцию длинной руки, например:
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;
}
Пример:
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") );
Стиль альтернативного кода:
В качестве альтернативы вышесказанному и моим личным предпочтениям вы можете абстрагировать части (проверка, обработка ошибок,преобразования) в дополнительные вспомогательные методы.
Это дает вам дополнительную гибкость в хранении этих частей отдельно для чистоты / повторного использования, если вы хотите, но цель состоит в том, чтобы упростить выполнение функции так, чтобы она была читаемой и легко поддерживать / изменять в будущем.
Я считаю этот стиль особенно полезным при работе с проектами, включающими Vue, React и другие библиотеки и инфраструктуры, ориентированные на состояние / рендеринг.
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 );
}
Пример:
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" ) );
Вот так ...
Я знаю, я знаю. Я написал довольно длинный ответ на этот простой вопрос- Я склонен увлекаться!В любом случае, было ли все из этого полезным для вас, надеюсь, я смог хоть немного помочь!
Удачи!