在TypeScript中使用forEach返回boolean

时间:2018-09-24 06:35:33

标签: javascript typescript

我想检查我的数组,如果我得到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;
    }

2 个答案:

答案 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;