我有一个以下对象的列表:
class Student : {
IndexNo : string;
Subject : string;
Name : string;
isAvailable : boolean
}
students: Student[];
我想检查“isAvailable”是否适用于任何学生。
你可以使用array.some
const found = yourArray.some(item => item.isAvailable == true);
你的定义是错误的。无论如何,你需要创建一个学生阵列。然后循环浏览它以查看是否有任何学生可用。如果提供的条件匹配,数组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);
如果学生是你的阵列:
检查所有数组是否有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)