将HTML集合减少为仅满足特定条件的元素

时间:2019-01-08 11:08:20

标签: javascript

labels HTML集合简化为仅textContext属性值与text_criteria数组中的任何字符串匹配的那些元素的最优雅的方法是什么?

var labels = document.getElementsByClassName('some-class-name')
var textContent_criteria = ['Description', 'Category', 'Department', 'Justification']

我可以使用多个for循环来做到这一点,但是我更喜欢以功能性编程为灵感的解决方案。

2 个答案:

答案 0 :(得分:0)

您可以.call Array.prototoype.filter函数:

var labels = document.getElementsByClassName('some-class-name');
var textContent_criteria = ['Description', 'Category', 'Department', 'Justification'];
var matchingLabels = Array.prototype.filter.call(
  labels,
  ({ textContent }) => textContent_criteria.includes(textContent)
);

console.log(matchingLabels);
<div class="some-class-name">Description</div>
<div class="some-class-name">Bad Description</div>
<div class="some-class-name">Category</div>

答案 1 :(得分:0)

您可以结合使用数组方法SELECT kk_aj_tbl_arrangemang.arrid FROM kk_aj_tbl_content INNER JOIN kk_aj_tbl_arridtocontent ON kk_aj_tbl_content.contentid = kk_aj_tbl_arridtocontent.contentid INNER JOIN kk_aj_tbl_arrangemang ON kk_aj_tbl_arridtocontent.arrid = kk_aj_tbl_arrangemang.arrid INNER JOIN kk_aj_tbl_arrangemangstatus ON kk_aj_tbl_arrangemang.arrangemangstatusid = kk_aj_tbl_arrangemangstatus.arrangemangstatusid INNER JOIN kk_aj_tbl_konstformtyp ON kk_aj_tbl_arrangemang.konstformid = kk_aj_tbl_konstformtyp.konstformid INNER JOIN users ON kk_aj_tbl_arrangemang.adminuserid = users.userid INNER JOIN kk_aj_tbl_utovare ON kk_aj_tbl_arrangemang.utovarid = kk_aj_tbl_utovare.utovarid WHERE ( kk_aj_tbl_arrangemang.arrangemangstatusid = @arrStatusTyp ) AND ( kk_aj_tbl_arridtocontent.version = 1 ) AND ( ( kk_aj_tbl_content.rubrik LIKE '%' + @searchval + '%' ) AND ( kk_aj_tbl_konstformtyp.konstformid = @roll1 ) OR ( kk_aj_tbl_content.rubrik LIKE '%' + @searchval + '%' ) AND ( kk_aj_tbl_konstformtyp.konstformid = @roll2 ) OR ( kk_aj_tbl_content.rubrik LIKE '%' + @searchval + '%' ) AND ( kk_aj_tbl_konstformtyp.konstformid = @roll3 ) OR ( kk_aj_tbl_content.rubrik LIKE '%' + @searchval + '%' ) AND ( kk_aj_tbl_konstformtyp.konstformid = @roll4 ) OR ( kk_aj_tbl_content.underrubrik LIKE '%' + @searchval + '%' ) AND ( kk_aj_tbl_konstformtyp.konstformid = @roll1 ) OR ( kk_aj_tbl_content.underrubrik LIKE '%' + @searchval + '%' ) AND ( kk_aj_tbl_konstformtyp.konstformid = @roll2 ) OR ( kk_aj_tbl_content.underrubrik LIKE '%' + @searchval + '%' ) AND ( kk_aj_tbl_konstformtyp.konstformid = @roll3 ) OR ( kk_aj_tbl_content.underrubrik LIKE '%' + @searchval + '%' ) AND ( kk_aj_tbl_konstformtyp.konstformid = @roll4 ) OR ( kk_aj_tbl_utovare.organisation LIKE '%' + @searchval + '%' ) AND ( kk_aj_tbl_konstformtyp.konstformid = @roll1 ) OR ( kk_aj_tbl_utovare.organisation LIKE '%' + @searchval + '%' ) AND ( kk_aj_tbl_konstformtyp.konstformid = @roll2 ) OR ( kk_aj_tbl_utovare.organisation LIKE '%' + @searchval + '%' ) AND ( kk_aj_tbl_konstformtyp.konstformid = @roll3 ) OR ( kk_aj_tbl_utovare.organisation LIKE '%' + @searchval + '%' ) AND ( kk_aj_tbl_konstformtyp.konstformid = @roll4 ) ) ORDER BY kk_aj_tbl_arridtocontent.datum DESC filter

some