将 class 添加到 div,其中属性值的部分匹配与单独数组中的任何值都不匹配

Add a class to a div where partial match of attribute value doesn't match any values in separate array

假设我有以下 div 数组,我已将其分配给 var fileTypes

<dd data-param-type="facetFilters" data-param-value="document_type:pdf">PDFs</dd>
<dd data-param-type="facetFilters" data-param-value="document_type:document">Documents</dd>
<dd data-param-type="facetFilters" data-param-value="document_type:spreadsheet">Spreadsheets</dd>
<dd data-param-type="facetFilters" data-param-value="document_type:presentation">Presentations</dd>
<dd data-param-type="facetFilters" data-param-value="document_type:image">Images</dd>

我还有以下对象:

{email: 80, pdf: 27}

如果 data-param-value 既不匹配 email 也不匹配 [=],那么遍历 fileTypes div 并添加 disabled 的 class 的最佳方法是什么? 20=]?

我正在使用以下内容在 :

之后提取参数值
var type = $(item).data('param-value');
var regex = /type:(.*)/;
var match = regex.exec(type);
console.log(match[1]);
=> pdf // (document, spreadsheet, etc)

我无法完全解决的是找到不包含匹配项的 div 的简洁方法。我已经破解了 Underscore 中的 _.filter_.difference,但无法正确处理。

您使用的是jQuery,所以可以使用filter方法来过滤匹配元素:

// get the object keys
var keys = Object.keys({email: 80, pdf: 27});

$('dd[data-param-value]').filter(function() {
   var key = $(this).data('param-value').split(':')[1];
   return $.inArray(key, keys) === -1;
}).addClass('disabled');

https://jsfiddle.net/vxduagky/