每个Jquery - 停止循环并返回对象

时间:2011-11-22 09:16:52

标签: jquery loops return each

任何人都可以告诉我为什么循环在5次进入后没有停止? http://jsbin.com/ucuqot/edit#preview


$(document).ready(function()
{
  someArray = new Array();
  someArray[0] = 't5';
  someArray[1] = 'z12';
  someArray[2] = 'b88';
  someArray[3] = 's55';
  someArray[4] = 'e51';
  someArray[5] = 'o322';
  someArray[6] = 'i22';
  someArray[7] = 'k954';  

  var test =  findXX('o322');   

});

function findXX(word)
{  
  $.each(someArray, function(i)
  {
    $('body').append('-> '+i+'<br />');
    if(someArray[i] == 'someArray')
    {
      return someArray[i]; //<--- did not stop the loop!
    }   
  });  
}

提前致谢!

6 个答案:

答案 0 :(得分:133)

因为当您在return循环中使用each语句时,“非假”值将充当continue,而false将充当break false。您需要从each函数返回function findXX(word) { var toReturn; $.each(someArray, function(i) { $('body').append('-> '+i+'<br />'); if(someArray[i] == word) { toReturn = someArray[i]; return false; } }); return toReturn; } 。像这样:

{{1}}

来自docs

  

我们可以在特定的迭代中打破$ .each()循环   回调函数返回false。返回非假是与a相同   在for循环中继续声明;它将立即跳到下一个   迭代。

答案 1 :(得分:2)

这里:

http://jsbin.com/ucuqot/3/edit

function findXX(word)
{  
  $.each(someArray, function(i,n)
  {
    $('body').append('-> '+i+'<br />');
    if(n == word)
    {
      return false;
    }   
  });  
}

答案 2 :(得分:1)

修改后的With Sheets("RTTRACK") wsReport.ChartObjects("Chart 3").Chart.SetSourceData Source:=.Range(.Cells(2, 9), .Cells(iRTNo + 1, 9)) End With 函数

$.each

它将在非假/非空结果上打破循环并将其返回,所以在你的情况下它将是

$.fn.eachReturn = function(arr, callback) {
   var result = null;
   $.each(arr, function(index, value){
       var test = callback(index, value);
       if (test) {
           result = test;
           return false;
       }
   });
   return result ;
}

答案 3 :(得分:0)

试试这个......

  someArray = new Array();
  someArray[0] = 't5';
  someArray[1] = 'z12';
  someArray[2] = 'b88';
  someArray[3] = 's55';
  someArray[4] = 'e51';
  someArray[5] = 'o322';
  someArray[6] = 'i22';
  someArray[7] = 'k954';  

  var test =  findXX('o322'); 
  console.log(test);



function findXX(word)
{  
  for(var i in someArray){


    if(someArray[i] == word)
    {
      return someArray[i]; //<---  stop the loop!
    }   
  }
}

答案 4 :(得分:0)

  

“我们可以通过以下方式在特定迭代中破坏$ .each()循环:   回调函数返回false。返回非假与   for循环中的continue语句;它将立即跳到   下一次迭代。”

来自http://api.jquery.com/jquery.each/

是的,这是旧的,只是回答这个问题,这可能会更简单:

function findXX(word) {
  $.each(someArray, function(index, value) {
    $('body').append('-> ' + index + ":" + value + '<br />');
    return !(value == word);
  });
}
$(function() {
  someArray = new Array();
  someArray[0] = 't5';
  someArray[1] = 'z12';
  someArray[2] = 'b88';
  someArray[3] = 's55';
  someArray[4] = 'e51';
  someArray[5] = 'o322';
  someArray[6] = 'i22';
  someArray[7] = 'k954';
  findXX('o322');
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

更多评论:

function findXX(myA, word) {
  let br = '<br />';//create once
  let myHolder = $("<div />");//get a holder to not hit DOM a lot
  let found = false;//default return
  $.each(myA, function(index, value) {
    found = (value == word);
    myHolder.append('-> ' + index + ":" + value + br);
    return !found;
  });
  $('body').append(myHolder.html());// hit DOM once
  return found;
}
$(function() {
  // no horrid global array, easier array setup;
  let someArray = ['t5', 'z12', 'b88', 's55', 'e51', 'o322', 'i22', 'k954'];
  // pass the array and the value we want to find, return back a value
  let test = findXX(someArray, 'o322');
  $('body').append("<div>Found:" + test + "</div>");
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>

注意:数组.includes()可能更适合这里https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes

或者只是.find()来获得https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find

答案 5 :(得分:0)

与其设置标志,不如使用JavaScript的Array.prototype.find在数组中查找匹配项,这可能会更优雅。从回调返回真实值后,循环将结束,并且迭代期间的数组值将成为.find调用的返回值:

function findXX(word) {
    return someArray.find((item, i) => {
        $('body').append('-> '+i+'<br />');
        return item === word;
    }); 
}

const someArray = new Array();
someArray[0] = 't5';
someArray[1] = 'z12';
someArray[2] = 'b88';
someArray[3] = 's55';
someArray[4] = 'e51';
someArray[5] = 'o322';
someArray[6] = 'i22';
someArray[7] = 'k954';

var test = findXX('o322');
console.log('found word:', test);

function findXX(word) {
  return someArray.find((item, i) => {
    $('body').append('-> ' + i + '<br />');
    return item === word;
  });
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>