我有以下对象的列表:
class Student : {
IndexNo : string;
Subject : string;
Name : string;
isAvailable : boolean
}
students: Student[];
我想检查“ isAvailable”是否适用于所有学生。
答案 0 :(得分:0)
您可以使用array.some
const found = yourArray.some(item => item.isAvailable == true);
答案 1 :(得分:0)
您的定义是错误的。无论如何,您需要创建一系列学生。然后遍历它,看是否有学生可用。如果提供的条件匹配,则数组Some
方法将退出循环。
private students: Array<{ IndexNo: string, Subject: string, Name: string, isAvailable: boolean }> = [];
const ifAnyTrue: boolean = this.students.some((student: { IndexNo: string, Subject: string, Name: string, isAvailable: boolean }) => student.isAvailable);
答案 2 :(得分:0)
如果学生是您的数组:
检查所有数组是否都有一个isAvailable true的学生:
const found = students.some(item => item.isAvailable == true)
使用isAvailable true获取第一个学生的索引(如果没有这样的元素,则返回-1):
const index = students.findIndex(item => item.isAvailable === true)
使用isAvailable true获取所有元素的数组:
const availableStudents = students.filter(item => item.isAvailable === true)