How to break or continue Ext.each

前提是你 提交于 2019-12-30 07:54:08

问题


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

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


回答1:


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.




回答2:


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



回答3:


Return false to 'break' and return anything but false to 'continue'.

var array = [1, 2, 3];
Ext.each(array, function(ele){
    console.log(ele);
    if(ele !== 2){
        return false;  // break out of `each` 
    }
})

Ext.each(array, function(ele){
     console.log(ele);
    if(ele !== 3){
        return true; // continue
    }
})


来源:https://stackoverflow.com/questions/1491982/how-to-break-or-continue-ext-each

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!