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)
};
Mathieu Rios
  • 350
  • 1
  • 17
  • 1
    Suggested reading: [Obscenity Filters: Bad Idea, or Incredibly Intercoursing Bad Idea?](https://blog.codinghorror.com/obscenity-filters-bad-idea-or-incredibly-intercoursing-bad-idea/) (by the founder of StackOverflow itself) – NullUserException Sep 30 '19 at 07:37
  • Possible duplicate of [How to filter an array from all elements of another array](https://stackoverflow.com/questions/34901593/how-to-filter-an-array-from-all-elements-of-another-array) – Dexygen Sep 30 '19 at 07: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 – Dexygen Sep 30 '19 at 07: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 – Code Maniac Sep 30 '19 at 07:41

4 Answers4

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.

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);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
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;
  })
};
Zero
  • 1
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);
Durgesh
  • 205
  • 2
  • 9