Angular 如何在TypeScript中过滤另一个对象数组的对象数组?
我知道如何通过Angular 如何在TypeScript中过滤另一个对象数组的对象数组?,angular,typescript,Angular,Typescript,我知道如何通过b.building.id在这里过滤预订: bookings.filter(b => b.building.id == this.filter.buildingId); 但是如果b.modules也是一个数组,那么如何过滤bookings对象数组。以下是我失败的尝试: bookings.filter(b => b.modules.programme.facultyId == this.filter.facultyId); bookings.filter(b =>
b.building.id
在这里过滤预订
:
bookings.filter(b => b.building.id == this.filter.buildingId);
但是如果b.modules
也是一个数组,那么如何过滤bookings
对象数组。以下是我失败的尝试:
bookings.filter(b => b.modules.programme.facultyId == this.filter.facultyId);
bookings.filter(b => {
for (let module of b.modules) {
module.programme.facultyId == this.filter.facultyId;
}
});
还有一次失败的尝试:
bookings.filter(b => b.modules.programme.facultyId == this.filter.facultyId);
bookings.filter(b => {
for (let module of b.modules) {
module.programme.facultyId == this.filter.facultyId;
}
});
更新:
下面是一个JSON中的预订结果示例:
[
{
"id": 2,
"semesterId": 1,
"room": {
"capacity": 17,
"code": null,
"building": {
"id": 5,
"name": "Faculty of Science"
},
"id": 15,
"name": "FSB 1.10"
},
"building": {
"id": 5,
"name": "Faculty of Science"
},
"bookDate": "2020-11-10T00:00:00",
"timeSlots": [
{
"id": 1,
"startTime": "1900-01-01T07:50:00",
"endTime": "1900-01-01T08:40:00"
},
{
"id": 2,
"startTime": "1900-01-01T08:50:00",
"endTime": "1900-01-01T09:40:00"
}
],
"modules": [
{
"id": 5,
"name": "Computer Systems and Information Technology",
"code": "SS-1202",
"lecturers": [
{
"id": 4,
"name": "Lim",
"title": "Dr."
}
],
"programme": {
"id": 1,
"name": "Computer Science",
"shortName": null,
"facultyId": 1
}
}
]
}
]
像这样试试
可能缺少两件事,第一个第二个筛选器在筛选器之后没有返回
,在某些情况下,它可能会错过此
引用
const that = this;
bookings.filter(b => {
return b.modules.filter(module.programme.facultyId == that.filter.facultyId)
});
如果module.program.facultyId是唯一的,您可以使用:
bookings.filter(b => b.modules.find(module => module.programme.facultyId == this.filter.facultyId));
以下是语法:
var result = bookings.filter(b => b.modules.filter(m => m.programme.facultyId === b.facultyId));