jQuery:打破嵌套的$ .each循环

时间:2012-04-20 23:50:00

标签: javascript jquery

我循环遍历列表中的行{/ 1}},并在每行上应用一组带$.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循环跳过该行?

1 个答案:

答案 0 :(得分:1)

如果至少有一个filtervalue不匹配的过滤器,你想跳过一行吗?然后不要跳过 iff filtervalue匹配至少一个过滤器。

$.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;
}