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

4

1 回答 1

1

如果至少没有一个过滤器filtervalue不匹配,您想跳过一行,对吗?如果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;
}
于 2012-04-20T23:56:35.707 回答