Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to break or continue Ext.each

Tags:

Ext.each(boundsExtend, function(value) {     if(value != record.ID) break; }); 

So how do I break or continue Ext.each loop?

like image 727
dfilkovi Avatar asked Sep 29 '09 11:09

dfilkovi


People also ask

Can we use continue and break statements with the forEach loop?

No, it doesn't, because you pass a callback as a return, which is executed as an ordinary function. All forEach does is call a real, actual function you give to it repeatedly, ignore how it exits, then calls it again on the next element.

Can we use break statement in forEach loop in JS?

There is no way to stop or break a forEach() loop other than by throwing an exception. If you need such behavior, the forEach() method is the wrong tool.


2 Answers

From the docs:

If the supplied function returns false, iteration stops and this method returns the current index.

So as in the OP's example (assuming record is in scope and non-null):

Ext.each(boundsExtend, function(value) {   if (value != record.ID) {     return false;   }   // other logic here if ids do match }); 

Note that returning false exits the loop completely, so in this case the first non-matching record will bypass any additional checking.

However I'm guessing that what you're really trying to do is loop until you find the matching record, do some logic, then short-circuit the loop. If that's the case, the logic would actually be:

Ext.each(boundsExtend, function(value) {   if (value === record.ID) {     // do your match logic here...     // if we're done, exit the loop:     return false;   }   // no match, so keep looping (i.e. "continue") }); 

Any other value that is not explicitly false (e.g. null by default) will keep the loop going.

like image 56
Brian Moeskau Avatar answered Oct 09 '22 11:10

Brian Moeskau


var countries = ['Vietnam', 'Singapore', 'United States', 'Russia'];  Ext.Array.each(countries, function(name, index, countriesItSelf) {     console.log(name); });  Ext.Array.each(countries, function(name, index, countriesItSelf) { if (name === 'Singapore') {     return false; // break here } }); 
like image 22
Anoop Pete Avatar answered Oct 09 '22 10:10

Anoop Pete