检查角度数组对象是否具有指定值

问题描述 投票:-1回答:3

我有一个以下对象的列表:

class Student : {
    IndexNo : string;
    Subject : string;
    Name : string;
    isAvailable : boolean
}
students: Student[];

我想检查“isAvailable”是否适用于任何学生。

angular arraylist angular6
3个回答
0
投票

你可以使用array.some

const found = yourArray.some(item => item.isAvailable == true);

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);

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) 
© www.soinside.com 2019 - 2024. All rights reserved.