我想检查我的数组,如果我得到3,我想破坏我的forEach,如果不是true,则返回true或false。
但是当我在html-
中按3时我变得不确定 let errorSave: boolean = this.checkParams();
console.log("errorSave" , errorSave); // UNDEFINED
checkParams() {
let errorSave = this.params.forEach(element => {
if (element.origin === 3) {
return true;
}
});
return errorSave;
}
答案 0 :(得分:4)
您尝试使用find
吗?
checkParams() {
return !!this.params.find(element => element.origin === 3);
}
答案 1 :(得分:0)
使用indexOf代替foreach子句。
array=[1,2,3];
return this.array.indexOf(3)>-1;