jQuery 过滤每个不为空的字符串

jQuery filter for each string that's not null

我如何为每个不为空的字符串应用 jQuery 过滤器?

基本上,我有以下代码,并希望它适用于预先设置的任意数量的 "selectors"。

可用的选择器列表将在一个数组中。

if (typeselector === '' && colorselector === '') {          
  $('.product').filter(selector).show();
} else if (typeselector === '') {
  $('.product').filter(selector).filter(colorselector).show();
} else if (colorselector === '') {
  $('.product').filter(selector).filter(typeselector).show();
} else {
  $('.product').filter(selector).filter(typeselector).filter(colorselector).show();
}

感谢任何suggestions/help!

为了简化,您可以使用链接:

var products = $('.product').filter(selector);
if (typeselector !== '') {          
  products = products.filter(typeselector);
}
if (colorselector !== '') {          
  products = products.filter(colorselector);
}
products.show();

甚至是这样的:

var products = $('.product').filter(selector);
var selectors = [typeselector, colorselector];

selectors.each(function(selector) {
    if (selector !== '') {
        products = products.filter(selector);
    }
});
products.show();