【发布时间】:2017-08-28 00:52:21
【问题描述】:
如何提前退出我的 TypeScript 文件中的函数?
checkIfFollowed(){
this.currentUserInfos.followed.forEach(element => {
if(18785 == 18785){
console.log('its true');
this.alreadyFollowed = true;
return; // Exit checkIfFollowed() here
}
});
this.alreadyFollowed = false;
console.log('the end');
return;
}
当我运行它时,它已完全执行,但它应该在第一次之后退出:
'这是真的'
但在我的控制台中,我得到:
这是真的
这是真的
结束
Foreach 按预期循环了 2 次,但是为什么该方法在点击“返回”后没有停止?
我不是想退出 forEach,而是在 foreach 中结束方法“checkIfFollowed”。
'the end' 不应该被打印出来。
【问题讨论】:
-
请您发布 currentUserInfos;
-
currentUserInfos.followed中有2个对象,只是嵌套对象,没什么特别的
-
使用 Array#some from here :developer.mozilla.org/en/Core_JavaScript_1.5_Reference/…
-
实际要求是什么?您将此处的 if 检查硬编码为
if(18785 == 18785)。我认为这不是您的实际要求。 -
我想知道如何在任何地方结束一个方法。
标签: javascript arrays foreach