1

I have an array of strings that I want to filter by another array of words. The goal is to remove the entire string if it contains one word of the second argument.

Let's take this as an example :

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']

My code is returning the full array of comments, I don't understand how I can filter through all the elements of the bannedWords array. I'm pretty sure I'm lacking of a basic thing right there but I'm stuck for a while now so... Thanks !

Here is what I coded :

  return comments.filter(comment => comment.includes(bannedWords) == false)
};
4
  • 1
    Suggested reading: Obscenity Filters: Bad Idea, or Incredibly Intercoursing Bad Idea? (by the founder of StackOverflow itself) Commented Sep 30, 2019 at 7:37
  • Possible duplicate of How to filter an array from all elements of another array Commented Sep 30, 2019 at 7:38
  • 1
    Did you research this? There are PLENTY of other answers to almost the same exact here on StackOverflow. Nobody should even be answering this question, everybody should be marking it as a duplicate Commented Sep 30, 2019 at 7:40
  • Your problem is here comment.includes(bannedWords) here you're passing complete array as parameter to includes where as you need to match pass individual name at a time Commented Sep 30, 2019 at 7:41

4 Answers 4

3
function filterOffensiveComments(comments, bannedWords) {
  return comments.filter(comment => !bannedWords.some(word => comment.includes(word)));
}

.includes only accepts a single String. Use some, not every, because it will early-return.

Sign up to request clarification or add additional context in comments.

2 Comments

code only aren't considered as good answer, Please add explanation why OP's code wasn't working why yours works.
@Code Maniac answer.includes( explanation ) === true
2

Check if .every of the banned words are not included in the comment you're iterating over. Make sure to call toLowerCase on the comment first:

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);

Or, if you want to construct a regular expression:

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);

Comments

0
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;
  })
};

Comments

0

Tried to implement using callback -

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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.