Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break from objx(data).each(function(item) iteration?

How to break from objx(data).each(function(item) iteration when a certain condition is met.

My Json string is below:

var data= [{"field1": "0","field2": "2"},{"field1": "7","field2": "2"},{"field1": "1","field2": "5"}];

Here is a my code:

function iterate(){
                objx(data).each(function(item){
                 if(item.field1 == "7"){
                   //doing some job;
                   return; 
                  }
                 alert("after if is executed");// this alert coming inspite of giving 
                });                            // return in if block
             }

i think the return is only enabling it to come out of function but not from the loop. i dont want this unnecessary iteration once my condition is met and job is done. can anyone suggest how to come out of this each loop?

like image 722
Debaprasad Jana Avatar asked Mar 11 '26 14:03

Debaprasad Jana


1 Answers

return false;

Returning false breaks out of the each function.

like image 143
Justice Erolin Avatar answered Mar 14 '26 04:03

Justice Erolin



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!