在Angular 8中,可以使用Array的includes()方法来检查数组中是否存在某个项。下面是一个示例代码:
// 在组件中定义一个数组
items: string[] = ['apple', 'banana', 'orange'];
// 在方法中检查数组中是否存在某个项
checkItem(item: string) {
if (this.items.includes(item)) {
console.log(item + ' exists in the array.');
} else {
console.log(item + ' does not exist in the array.');
}
}
在上述示例中,我们定义了一个名为items的字符串数组,并在checkItem()方法中使用includes()方法来检查数组中是否存在某个项。如果项存在于数组中,将会输出该项存在的信息;如果项不存在于数组中,则会输出该项不存在的信息。
希望对你有所帮助!