简体   繁体   English

如何完全从函数内部的.forEach内部退出

[英]How can I completely exit from inside a .forEach that is inside a function

I have this method: 我有这种方法:

    wordFormDirty = (): boolean => {
    var self = this;
    angular.forEach(self.word.wordForms, function (wf, key) {
        var wordFormNgForm = 'wordFormNgForm_' + wf.wordFormId
        if (!self[wordFormNgForm].$pristine) {
            return true;
        }
    });
    return false;
};

From what I see this never returns true. 从我的角度来看,这永远不会返回true。 Can someone give me advice as to how I can implement this so that a form that's not pristine will make the wordFormDirty() method return true. 有人可以给我一些建议,让我了解如何实现此方法,以便不是原始格式的表单使wordFormDirty()方法返回true。

If you wish to get a result directly from walking the Array , consider using other methods than forEach . 如果希望直接通过遍历Array获得结果,请考虑使用forEach以外的其他方法。 Eg: 例如:

return Object.values(this.word.wordForms).some(
  ({ wordFormId }) => !this[`wordFormNgForm_${wordFormId}`].$pristine
);

can you try this, in this case, if I've undestand the issue, the first time there is a value true the result is set to true otherwise it remains false 您能尝试这种情况吗?在这种情况下,如果我无法理解问题,那么第一次出现值true时,结果将设置为true,否则将保持为false

wordFormDirty = (): boolean => {
    var self = this;
    var result = false;
    angular.forEach(self.word.wordForms, function (wf, key) {
        var wordFormNgForm = 'wordFormNgForm_' + wf.wordFormId
        if (!self[wordFormNgForm].$pristine) {
            result = true;
        }

    });
    return result;
};

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何从 map 函数中的 forEach 返回一个 div? - How can I return a div from forEach inside map function? 我如何从另一个 function 中的 function 退出(返回) - how do I exit (return) from a function inside a another function 如何从其中的 for 循环中退出(转义)function? - How to exit (escape) a function from for loop inside of it? 如何防止 forEach 循环内的 function 被多次调用? (在大多数情况下,它会重复出现) - How can I prevent function inside of forEach loop from being called more than once? (For the most part it repeats predictably) 如何从要返回的函数内部的函数内部返回函数的值 - How can I return a value for a function from inside a function inside the function I wish to return 我怎样才能让我的jQuery函数可用于foreach循环中的每个元素? - How can I get my jQuery function for work for each element inside a foreach loop? 如何从类中的另一个函数调用函数 - How can I call a function from another function inside a Class 从超时内部退出功能 - Exit from function from inside Timeout 如何获取forEach循环中的值的索引? - How can I get index of the value inside forEach loop? 如何从 Ajax 在打字稿中调用的函数内部调用类中的函数? - How can I call a function inside a class from inside a function called up by Ajax in typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM