如何根据另一个数组中的单词过滤一个数组?

时间:2018-10-10 20:25:00

标签: javascript filter word

我想过滤包含在字符串中的一些单词。例如:

输入

1。 “消失”的定义:

[ "Cease to be visible.",
  "Cease to exist or be in use.",
  "Be lost or go missing, become IMPOSSIBLE to find.",
  "Abduct or arrest and kill or detain (a person) for political reasons,
  without making their fate known." ]

2。禁词:

[ 'without', 'impossible' ]

结果

[ "Cease to be visible.", "Cease to exist or be in use." ]

我认为,我非常接近答案:

function filterDefinition (defs, badWords) {
  const definitionFilter = defs.filter(function(def) {
    if (def.includes(badWords || badWords.toUpperCase()) {
      return !defs
    }
  });
  return definitionFilter;
}

1 个答案:

答案 0 :(得分:0)

喜欢吗?

function filterDefinition (defs, badWords) {
  return defs.filter(function(def) {
    return !badWords.some(badWord => def.includes(badWord) || def.includes(badWord.toUpperCase()));
  });
}

String.includes(string)在数组上不起作用。