如何在Javascript中从interator内部返回值

如何在Javascript中从interator内部返回值,javascript,jquery,Javascript,Jquery,我有一个函数,我想返回true或false 在函数内部,我遍历页面上的一些元素并检查它们的值 但是如果我在迭代器中放一个return语句,它将从匿名函数返回,对吗?(而不是外部功能) 所以不管发生什么,这个函数总是返回false 最好的方法是什么?下面的解决方案似乎有效,但似乎不太优雅 function someElementHasZeroValue() { elements_found_with_zero_value = 0; $('.some-class').each(functio

我有一个函数,我想返回true或false

在函数内部,我遍历页面上的一些元素并检查它们的值

但是如果我在迭代器中放一个return语句,它将从匿名函数返回,对吗?(而不是外部功能)

所以不管发生什么,这个函数总是返回false

最好的方法是什么?下面的解决方案似乎有效,但似乎不太优雅

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;
  }
}

更一般地说,是否有人知道为什么Javascript要求您使用匿名函数迭代元素,而不是像大多数语言那样使用普通的迭代器,或者是否有某种方法可以做到这一点,我不知道?

您需要一个像您这样的解决方案,但它可以简单一点

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"
}
此外,如果要在
if()
成功时中断
.each()
循环,可以添加一个
返回false,它将在不返回函数的情况下中断循环

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

你需要一个像你一样的解决方案,但它可以简单一点

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"
}
此外,如果要在
if()
成功时中断
.each()
循环,可以添加一个
返回false,它将在不返回函数的情况下中断循环

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

好吧,JavaScript不需要你这么做。是jQuery在做这件事。使用JavaScript,您可以对循环执行常规操作,然后以这种方式返回。jQuery的
each()
将递增地返回一个jQuery对象,用于您使用的选择器的所有匹配项。你可以这样做:

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

好吧,JavaScript不需要你这么做。是jQuery在做这件事。使用JavaScript,您可以对循环执行常规操作,然后以这种方式返回。jQuery的
each()
将递增地返回一个jQuery对象,用于您使用的选择器的所有匹配项。你可以这样做:

function someElementHasZeroValue() {
  var returnValue = false;
  $('.some-class').each(function(){
    if($(this).html() == '0'){
      returnValue = true;
    }
  });
  return returnValue;
}
请注意,
returnfalse
内部的
每个
回调意味着我们应该中断迭代(参见jQuery文档),这类似于“正常循环”中的
break

也总是喜欢使用
var
声明变量(使用局部变量而不是全局变量)

请注意,
returnfalse
内部的
每个
回调意味着我们应该中断迭代(参见jQuery文档),这类似于“正常循环”中的
break


也总是喜欢使用
var
(使用局部变量而不是全局变量)声明变量。

您使用的是jQuery,而不是纯Javascript。您可以执行正常循环:

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

您使用的是jQuery,而不是纯Javascript。您可以执行正常循环:

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