简体   繁体   English

如何摆脱$ .each循环?

[英]how to break from $.each loop?

I have a jquery loop where I am slicing the json array upto 5 and now I need to check whether the key value is empty or not. 我有一个jquery循环,其中将json数组切成5,现在我需要检查键值是否为空。 Once I check for 5 elements,if my condition works then it will print 5 data but what if it doesn't? 一旦我检查了5个元素,如果我的条件正常,它将打印5个数据,但是如果不打印该怎么办? It shows me same amount of time the else. 它向我显示了其他时间相同的时间。

Here is my jquery code: 这是我的jQuery代码:

$.each(data.slice(0,5), function(key, value) {
              if(value.emailID!="")
              {
                $("#result-success").append("<p><a href='javascript:void(0)'>"+value.fullName+"<span>"+value.emailID+"</span></a></p>").addClass('before-search-message');
                $("#loader_gif").hide();    
              }
              else
              {
                $("#loader_gif").hide();
                $("#search-btn").removeAttr("disabled");
                $("#result-success").append("<p><a href='javascript:void(0)'>No Records Found!</span></a></p>").addClass('before-search-message'); 
              }
             });

If my emailID is blank for all array elements, then it appends for 5 times with message "No records found" any solution to that? 如果我的emailID对于所有数组元素都是空白,那么它将附加5次并显示消息“找不到记录”,对此有什么解决办法?

return false; = break;

return true; or return; = continue;

From Jquery Docs http://api.jquery.com/jQuery.each/ 从Jquery Docs http://api.jquery.com/jQuery.each/

let found = false;
$.each(data.slice(0,5), function(key, value) {

              if(value.emailID!="")
              {
                $("#result-success").append("<p><a href='javascript:void(0)'>"+value.fullName+"<span>"+value.emailID+"</span></a></p>").addClass('before-search-message');
                $("#loader_gif").hide();
                found = true;
              }
             });

if(!found){

                $("#loader_gif").hide();
                $("#search-btn").removeAttr("disabled");
                $("#result-success").append("<p><a href='javascript:void(0)'>No Records Found!</span></a></p>").addClass('before-search-message'); 

}

in my case, i think, 就我而言,我认为,

var valid = false;
$.each(data.slice(0,5), function(key, value) {
    if(value.emailID!="") {
        $("#result-success").append("<p><a href='javascript:void(0)'>"+value.fullName+"<span>"+value.emailID+"</span></a></p>").addClass('before-search-message');
        $("#loader_gif").hide();
        valid = true;
}
});

if (!valid) {
    $("#loader_gif").hide();
    $("#search-btn").removeAttr("disabled");
    $("#result-success").append("<p><a href='javascript:void(0)'>No Records Found!</span></a></p>").addClass('before-search-message');
}

I hope It's what you want. 我希望这就是你想要的。

var hasData =false
$.each(data.slice(0,5), function(key, value)     {
          if(value.emailID!="")
          {
           hasData = true
            $("#result-success").append("<p><a   href='javascript:void(0)'>"+value.fullName+"<span>"+value.emailID+"</span></a></p>").addClass('before-search-message');
            $("#loader_gif").hide();    
          }

         });
       if(!hasData){
          $("#loader_gif").hide();
            $("#search-btn").removeAttr("disabled");
            $("#result-success").append("<p><a href='javascript:void(0)'>No  Records Found!</span></a></enter code herep>").addClass('before-search-message');}

This is probably what you're looking for. 这可能是您要寻找的。 You were iterating over display no results 5 times 您遍历显示无结果5次

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM