【问题标题】:How to filter an array with elements of another array?如何用另一个数组的元素过滤一个数组?
【发布时间】:2019-09-30 07:29:22
【问题描述】:

我有一个字符串数组,我想用另一个单词数组进行过滤。 如果它包含第二个参数的一个单词,则目标是删除整个字符串。

我们以此为例

comments = [ "Very useful tutorial, thank you so much!",
  "React is not a damn framework, it's a LIBRARY"
  "Why you put bloody kitten pictures in a tech tutorial is beyond me!",
  "Which one is better, React or Angular?",
  'There is no "better", it depends on your use case, DAMN YOU'
]

bannedWords = ['bloody', 'damn']

我的代码返回了comments 的完整数组,我不明白如何过滤bannedWords 数组的所有元素。 我很确定我缺乏基本的东西,但我现在被困了一段时间......谢谢!

这是我的代码:

  return comments.filter(comment => comment.includes(bannedWords) == false)
};

【问题讨论】:

标签: javascript arrays filter


【解决方案1】:
function filterOffensiveComments(comments, bannedWords) {
  return comments.filter(comment => !bannedWords.some(word => comment.includes(word)));
}

.includes 只接受一个String。 使用some,而不是every,因为它会提前返回。

【讨论】:

  • code only 不被认为是好的答案,请添加解释为什么 OP 的代码不起作用为什么你的代码起作用。
  • @Code Maniac answer.includes( 解释 ) === true
【解决方案2】:

检查.every 的禁用词是否未包含在您正在迭代的评论中。请务必先在评论中致电toLowerCase

const comments = ["Very useful tutorial, thank you so much!", "React is not a damn framework, it's a LIBRARY",
  "Why you put bloody kitten pictures in a tech tutorial is beyond me!", "Which one is better, React or Angular?", 'There is no "better", it depends on your use case, DAMN YOU'];
const bannedWords = ['bloody', 'damn'];

const result = comments.filter(comment => bannedWords.every(word => !comment.toLowerCase().includes(word)))
console.log(result);

或者,如果你想构造一个正则表达式:

const comments = ["Very useful tutorial, thank you so much!", "React is not a damn framework, it's a LIBRARY",
  "Why you put bloody kitten pictures in a tech tutorial is beyond me!", "Which one is better, React or Angular?", 'There is no "better", it depends on your use case, DAMN YOU'];
const bannedWords = ['bloody', 'damn'];
const re = new RegExp(bannedWords.join('|'), 'i');

const result = comments.filter(comment => !re.test(comment));
console.log(result);

【讨论】:

    【解决方案3】:
    function filterOffensiveComments(comments, bannedWords) {
      return comments.filter(item=>{
        let shouldDelete = false;
        bannedWords.forEach(word=>{
            if(item.includes(word)){
                shouldDelete = true;
            }
        })
        if(shouldDelete){
            return null
        }
        return item;
      })
    };
    

    【讨论】:

      【解决方案4】:

      尝试使用回调实现 -

      const comments = [ "Very useful tutorial, thank you so much!",
        "React is not a damn framework, it's a LIBRARY",
        "Why you put bloody kitten pictures in a tech, tutorial is beyond me!",
        "Which one is better, React or Angular?",
        'There is no "better", it depends on your use case, DAMN YOU'
      ];
      
      const bannedWords = ['bloody', 'damn'];
      
      function getFilteredResults(item, index, array){
      var flag = 0;
      bannedWords.forEach(bannedWord => {
      if(item.toLowerCase().indexOf(bannedWord.toLowerCase()) != -1)
          flag = 1;
      });
      return flag == 0;
      }
      
      comments.filter(getFilteredResults);
      

      【讨论】:

        猜你喜欢
        • 2013-02-26
        • 1970-01-01
        • 1970-01-01
        • 2016-04-26
        • 1970-01-01
        • 2015-12-14
        • 1970-01-01
        • 1970-01-01
        • 2018-03-07
        相关资源
        最近更新 更多