简体   繁体   English

在JavaScript中从数组中获取对象

[英]Getting an object out of an array in javascript

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 ). 我有一个对象数组(具有属性如studentIDgradedobregisteredDate学生对象),还有另一个对象数组(具有属性studentIDbookIDbookISBN books对象)。

It's a Web app for managing the library at a small school. 这是一个用于管理小型学校图书馆的Web应用程序。 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). 我想做的是,当学生studentID 4的学生借用了一本书(例如标题为Welcome Home )时,当您尝试借书时(当然是一本副本,因为图书馆有很多库存)对于其他人,具有学生studentID 4的学生不应出现在有资格获得该书的学生列表中(除非该学生首先退还该书)。

The booksList Array is like below: booksList数组如下所示:

[
{
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 . 链接两个数组对象及其中的对象的对象是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 studentID s are in booksList . 阵列( studentListbooksList )被动态地提取和我不能告诉哪个studentID s为在booksList

How can I get this to work as I want them? 我怎样才能使其按需运行?

return !booksList.includes(el.studentID);

should be 应该

return !booksList.map(i => i.studentID).includes(el.studentID);

As a couple people said in the comments of your question, the problem is your code is expecting booksList to be an array of studentID s. 就像一对人在对您的问题的评论中所说的那样,问题是您的代码期望booksList是一个studentID的数组。 Since it's actually a list of books that have been checked out by students, you first need to make an array of all studentID s in booksList then you can use includes on the resulting array. 由于它实际上是学生已经签出的书的列表,因此您首先需要在booksListbooksList所有studentID的数组,然后可以在结果数组中使用includes See map . 地图

You can use Rocky Sims' solution or you can try this as well 您可以使用Rocky Sims的解决方案,也可以尝试使用

var legitStudents = studentList.filter(student => {
  return !booksList.filter(book => book.studentID === student.studentID).length;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM