Extjs 如何中断或继续Ext.each

Extjs 如何中断或继续Ext.each,extjs,Extjs,那么如何中断或继续Ext.each循环?从: 如果提供的函数返回 false,迭代停止,此方法无效 返回当前索引 因此,在OP的示例中(假设记录在范围内且非空): 请注意,返回false将完全退出循环,因此在这种情况下,第一条不匹配的记录将绕过任何附加检查 然而我猜你真正想做的是循环,直到你找到匹配的记录,做一些逻辑,然后短路循环。如果是这样的话,逻辑实际上是: Ext.each(boundsExtend, function(value) { if (value != record.ID)

那么如何中断或继续Ext.each循环?

从:

如果提供的函数返回 false,迭代停止,此方法无效 返回当前索引

因此,在OP的示例中(假设
记录
在范围内且非空):

请注意,返回
false
将完全退出循环,因此在这种情况下,第一条不匹配的记录将绕过任何附加检查

然而我猜你真正想做的是循环,直到你找到匹配的记录,做一些逻辑,然后短路循环。如果是这样的话,逻辑实际上是:

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

任何其他未显式为
false
(例如默认情况下为
null
)的值将保持循环继续。

false
返回到“中断”,并将
false以外的任何值返回到“继续”

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

返回除false以外的任何内容。continue对我无效。你能帮助我怎么做吗?这里有一个类似的例外,我没有这样做,并删除了一个对象。因此,itemId不在那里。“未捕获的TypeError:无法读取未定义的属性'itemId'”
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
}
});
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
    }
})