开发者

How to return a value from inside an interator in Javascript

I have a function that I want to return true or false.

Inside the function I iterate through some elements on the page and check their values.

But if I put a return statement inside the iterator, it will return from the anonymous function right? (and not the outer function).

function someElementHasZeroValue() {
  $('.some-class').each(function(){
    if($(this).html() == '0'){
      return true;
    }
  });
  return false;
}

So this function always returns false no matter what.

What is the best way to do this? The solution below seems to work but it doesn't seem very elegant.

function someElementHasZeroValue() {
  elements_found_with_zero_value = 0;
  $('.some-class').each(function(){
    if($(this).html() == '0'){
      elements_found_with_zero_value += 1;
    }
  });

  if(elements_found_with_zero_value > 0){
    return true;
  }else {
    return false;
  }
}

More generally, does anyone know why Javasc开发者_StackOverflowript requires you to iterate through elements with an anonymous function as opposed to a normal iterator like most languages, or is there some way to do it I'm not aware of?


You'll need a solution like yours, but it could be a little simpler.

function someElementHasZeroValue() {
      // this is the return value, initialized as false
  var ret = false;
  $('.some-class').each(function(){
    if($(this).html() == '0'){
      ret =  true; // set it to true
    }
  });
  return ret;  // return the value of "ret"
}

Additionally, if you wanted to break the .each() loop when the if() succeeds, you can add a return false;, which will break the loop without returning the function.

    if($(this).html() == '0') {
      ret = true;  // set it to true
      return false; // break the each() loop
    }


You are using jQuery, not pure Javascript. You could do a normal loop:

function someElementHasZeroValue() {
    var selection = $('.some-class');
    for (var i = 0; i < selection.length; i++) {
        if ($(selection[i]).html() === '0') {
            return true;
        }
    }
    return false;
}

Alternatively, you could do this in a more jQuery-like way:

function someElementHasZeroValue() {
    return $('.some-class').filter(function(){
        return $(this).html() === '0';
    }).length > 0;
}


Well, JavaScript doesn't require this of you. It's jQuery that is doing it. With JavaScript you could do normal for loops and return out of it that way. jQuery's each() will incrementally return a jQuery object for all matches of the selector you use. You could do something like this:

function someElementHasZeroValue() {
  var returnValue = false;
  $('.some-class').each(function(){
    if($(this).html() == '0'){
      returnValue = true;
    }
  });
  return returnValue;
}



function someElementHasZeroValue() {
  var result = false;

  $('.some-class').each(function() {
     if (($this).html() == '0') {
       result = true;
       return false;
     }
  });

  return result;
}

Please note that return false inside each callback means that we should interrupt iterations (see jQuery documentation), it's similar to break in "normal cycles".

Also always prefer variables declaration with var (use local instead of global variables).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜