firebase 在 forEach 中使用条件语句时,如何检测是否每个孩子都未通过条件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10790124/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 16:34:47  来源:igfitidea点击:

When using conditional statements inside of forEach, how to detect if every child failed the condition?

javascriptforeachconditionalfirebase

提问by ehabd

Starting with a Firebase DataSnapshot, I want something so that, in plain English, "if all the children failed the condition, execute something".

从 Firebase DataSnapshot 开始,我想要一些东西,用简单的英语来说,“如果所有孩子都没有达到条件,就执行一些东西”。

Here is what I have now:

这是我现在所拥有的:

     appleappsRef.on('value', function (allApplesSnapshot){
            allApplesSnapshot.forEach(function (appleSnapshot) {
                if (condition) {
                   //execute code
                } 
            });             
      });   

采纳答案by Michael Lehenbauer

Per the docs, your forEach callback can return true to cancel enumeration and forEach() will return true to signal that enumeration was canceled.

根据文档,您的 forEach 回调可以返回 true 以取消枚举,而 forEach() 将返回 true 以表示枚举已被取消。

This means you can do:

这意味着您可以:

appleappsRef.on('value', function (allApplesSnapshot){
  var foundOne = allApplesSnapshot.forEach(function (appleSnapshot) {
    if (condition) {
       return true; // found one, cancel enumeration
    }
  });
  if (!foundOne) {
    // all children failed the condition.
  }
});

回答by jfriend00

You can do it with .forEach using a boolean to keep track if you ever found the condition to be true:

如果您发现条件为真,您可以使用 .forEach 使用布尔值来跟踪:

 appleappsRef.on('value', function (allApplesSnapshot){
        var foundOne = false;
        allApplesSnapshot.forEach(function (appleSnapshot) {
            if (condition) {
               foundOne = true;
            } 
        });             
        if (!foundOne) {
            // all children failed the condition
        }
  });   

回答by Casey Chu

if (allApplesSnapshot.every(function (appleSnapshot) { return !condition; }))
    // ...

Array.everyreturns trueif every element of the array satisfies the function you pass it (that is, the function returns true when passed that element). So to test if every element failsa condition, then simply negate the condition within the function.

Array.every返回true如果数组满足每一个元素传递给它(即,当通过该元素的函数返回true)的功能。因此,为了测试,如果每一个元素出现故障的情况,然后简单地否定函数内的条件。

Note that some browsers don't supportArray.every, but every browser that supports Array.forEachshould also support Array.every.

请注意,有些浏览器不支持Array.every,但每个支持的浏览器也Array.forEach应该支持Array.every

回答by PaulG

Well, you could always loop through it and at any moment one of the children passes break out of the method somehow, otherwise fire the method that should be fired when all children fail. You can even use a boolean in this case.

好吧,您总是可以循环遍历它,并且在任何时候,其中一个子进程以某种方式从该方法中脱离出来,否则会在所有子进程都失败时触发应该触发的方法。在这种情况下,您甚至可以使用布尔值。

Ex: Make a boolean and call it allChildrenFailed and set it to true by default. Go through the foreach loop, and if any child passes change it to false. Then fire the method you need after the loop only if allChildrenFailed is true.

例如:创建一个布尔值并将其命名为 allChildrenFailed 并默认将其设置为 true。执行 foreach 循环,如果有任何子级通过,则将其更改为 false。然后仅当 allChildrenFailed 为真时才在循环后触发您需要的方法。