I have an array of objects (student objects which have properties like studentID, grade, dob, registeredDate) and I have another array of objects (books objects which have properties studentID, bookID, bookISBN).
It's a Web app for managing the library at a small school. What I want to do is that when a book (say has title Welcome Home) has been borrowed by student with studentID 4, when you try to lend that book (of course a copy since the library can have a number of them in stock) to someone else, the student with studentID 4 shouldn't be available in the list of students who are eligible to get that book (unless if the student had returned the book first).
The booksList Array is like below:
[
{
bookEdition: "2nd edition",
bookISBN: 9876533278765,
bookStatus: 0,
bookTitle: "Real Machines",
dateTaken: "2018-10-28",
returnDate: "2018-11-27",
studentID: "0000003"
},
{
bookEdition: "2015 edition",
bookISBN: 9876532226712,
bookStatus: 0,
bookTitle: "Real Machines",
dateTaken: "2018-08-28",
returnDate: "2018-09-27",
studentID: "0000004"
}
];
The studentsList is as below:
[
{
bio: "Needs extra work. Has problems with mathematics",
birthday: "2005-05-12",
className: "grade 5",
fullname: "Bridget Friday",
gender: "Female",
parentName: "Josam Friday",
studentID: "0000003",
studentStatus: "approved"
},
{
bio: "A bit naughty but intelligent. Pay close attention to his diet.",
birthday: "2003-11-07",
className: "grade 6",
fullname: "Charles Ben",
gender: "Male",
parentName: "Chris Ben",
studentID: "0000004",
studentStatus: "approved"
}
];
Now, I was trying to use the filter function but it doesn't give me the results I want. The thing that links the two array objects and the objects in them is the studentID.
I tried
var legitStudents = studentsList.filter(el => {
return !booksList.includes(el.studentID);
});
The above doesn't work. The arrays (studentList and booksList) are fetched dynamically and I can't tell which studentIDs are in booksList.
How can I get this to work as I want them?
studentsListandbooksListis. I'd tend to assumebooksListisn't an array of student IDs, which is what your code expects it to be. Please update your question with a minimal reproducible example demonstrating the problem, ideally a runnable one using Stack Snippets (the[<>]toolbar button; here's how to do one).!booksList.includes(el.studentID);this isn't right sincebooksListis not an array ofstudentIDs. It's an array objects