2012-04-20 135 views
1

我正在循环显示列表中的行w/$.each,并在每行应用一组筛选器$.each。我想跳过不匹配的行。这有点类似于:

$.each(data, function(i, row) { 
    count  = parseInt(row['n']); 
    year  = row['year']; 

    if (options.filters) { 
     $.each(options.filters, function(filter, filtervalue) { 
      if (row[filter] != filtervalue) return true; 
     }); 
    } 

    // Will only get here if all filters have passed 
} 

我怎样才能得到嵌套循环$.each跳过如果filtervalue不匹配给定的过滤器行?

回答

1

如果至少没有一个filtervalue不匹配的过滤器,您想要跳过一行,对不对?然后不要跳过一行ifffiltervalue匹配至少一个过滤器。

$.each(data, function(i, row) { 
    count  = parseInt(row['n']); 
    year  = row['year']; 


    // if there are no filters, don't skip the row (right? ;-) 
    var skipRow = !!options.filters; 

    if (options.filters) { 
     $.each(options.filters, function(filter, filtervalue) { 
      if (row[filter] == filtervalue) { 
       skipRow = false; 
      } 
     }); 
    } 

    if (skipRow) return true; 
}