代码之家  ›  专栏  ›  技术社区  ›  ParoX

如何从.each()中分离并返回函数的值

  •  43
  • ParoX  · 技术社区  · 14 年前

    我想用 return false 同时返回一个值。我该怎么做?

    function HasStores(state) {
        var statehasstores = false;
    
        $(stores).each(function (index, store) {
            if (state == store.state && store.category == "meyers") {
                statehasstores = true;
                return false;  // break
            }
        });
    
        return statehasstores;
    }
    

    我想在伪代码中做的是:

    Function () {
        for() {
            if found {
                return true;
            }   
        }
        return false;
    }
    
    8 回复  |  直到 7 年前
        1
  •  44
  •   Å ime Vidas Zim84    14 年前

    你做得对。。。

    引用自 http://api.jquery.com/each/

    “我们可以通过返回false从回调函数中停止循环。”

        2
  •  15
  •   mhitza Federico Taschin    14 年前

    有创造力:

    try {
      $(stores).each(function (index, store) {
        if(state == store.state && store.category == "meyers"){
          throw store;
        }
      });
    }
    catch(e) {
      // you got e with the value
    }
    

    不,我只是开玩笑,别用这个:)。这是我喜欢分享的一个想法。

        3
  •  13
  •   BrunoLM    14 年前

    var value;
    
    $(stores).each(function (index, store) {
        if(state == store.state && store.category == "meyers"){
            statehasstores = true;
            value = store; // added line
            return false; //break
        }
    });
    
    alert(value);
    

    你的方式很好。 I've tested on jsFiddle, see an example here .

    对你不起作用?你能展示更多的内容吗?

        4
  •  5
  •   reformed Ezequiel García    8 年前

    或者,您可以使用 for 循环而不是 each() ,然后返回值。

        5
  •  2
  •   Koobz    14 年前

    你的建议是怎么做的。我不认为这是权宜之计,而认为它更像是一个成语。

        6
  •  1
  •   elkolotfi    9 年前

    • 当jquery doc说:“我们可以通过返回false来停止回调函数中的循环。”

      函数(){ 如果找到{ 返回真值;
      } 返回false; }

    因此,为了使你的职能发挥作用,我提议这样做:

    Function () {
       variable found = false;
    foreach() {
        if found {
            found = true;
            return false; // This statement doesn't make your function return false but just cut the loop
        }   
    }
    return found;
    

    当然还有很多其他的方法来实现这一点,但我认为这是最简单的一种。

    库帕-别紧张!

        7
  •  1
  •   mike rodent    7 年前

    怎么样:

    $.each( myObj, function( key, value ){
      ...
      if( sthg... ){
        myObj.somethingWentHorriblyWrong = true;
        return false;
      }
    });
    
    if( myObj.somethingWentHorriblyWrong ){
      // ... do something, not forgetting to go:
      delete myObj.somethingWentHorriblyWrong;
    }
    

    我最初对什么感兴趣 $.each(... 真的回来了。就像上面说的 relevant JQ page ,方法返回其第一个参数,即已迭代的对象,但实际上解决方案甚至不需要使用该事实。。。

    PPS需要一个返回值的函数?当然,包装在一个外部功能。

        8
  •  0
  •   One_Dead_Headphone    8 年前

    正如其他人从 jQuery Each ,返回false只会中断循环而不返回值,但返回true将“继续”并立即开始下一次迭代。有了这些知识,您可以像这样简化代码:

    function HasStores(state) {
      var statehasstores = false;
    
      $(stores).each(function (index, store){
    
        // continue or break;
        statehasstores = !(state == store.state && store.category == "meyers"))
        return statehasstores;
      });
    
      return !statehasstores;
    }
    

    当然,使用double-negative有点傻,它的副作用是为每次错误的迭代将'true'保存到statehastores,反之亦然,但是最终的结果应该是相同的,并且不再有if语句。