0

I need help on filtering a Javascript array to remove names that DO NOT include at least one of a series of letters listed in a second array. I have tried over and over, using combinations of filter(), and also nested FOR loops. EXAMPLE:

name = ["chuck","lina","wanda","denise","reggie","candy"];
letter = ["h","l","z","e"];

The array returned by filter() should be:
nametwo = ["wanda","candy"];

It's simple to explain but I just cannot make it work in code. I've found many examples of filter() on here but none has helped. Thanks!

1
  • HI all - thanks for your responses. All good... <3<3<3 you all. :-) Commented Feb 5, 2022 at 17:39

4 Answers 4

1

Basically the same question here

const name = ["chuck","lina","wanda","denise","reggie","candy"];
const letter = ["h","l","z","e"];

const checker = value =>
  !letter.some(element => value.includes(element));

console.log(name.filter(checker));

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

Comments

1

For searching letters inside a string, you should use indexOf function.

If you want one line solution you can do it this way:

const names = ["chuck","lina","wanda","denise","reggie","candy"];
const letters = ["h","l","z","e"];
const result = names.filter(name => letters.filter(letter => name.indexOf(letter) !== -1).length === 0);

console.log(result)

And you can do it use two cycles:

const names = ["chuck","lina","wanda","denise","reggie","candy"];
const letters = ["h","l","z","e"];

const result = [];
for (let i = 0; i < names.length; i++) {
    let isContainLetter = false;
    for (let j = 0; j < letters.length; j++) {
      if (names[i].indexOf(letters[j]) !== -1) {
        isContainLetter = true;
        break;
      }
    }
  if (!isContainLetter) result.push(names[i]);
}

console.log(result)

1 Comment

I like this answer because it explains the logic, not just provides code.
1

You could filter with every and check if the name does not contains letters.

const
    names = ["chuck", "lina", "wanda", "denise", "reggie", "candy"],
    letters = ["h", "l", "z", "e"],
    result = names.filter(n => letters.every(l => !n.includes(l)));

console.log(result); // ["wanda", "candy"]

1 Comment

Brilliant, absolutely brilliant. Works like a charm. Wow. Thank you!
0

We can use :

  • Array.every() method to test whether all elements in the array pass the test implemented by the provided function or not. It returns a Boolean value.
  • Then we can use Array.filter() method which creates a new array with all elements that pass the test implemented by the provided function.

Working Demo :

const name = ["chuck","lina","wanda","denise","reggie","candy"];
const letter = ["h","l","z","e"];

const resultArr = name.filter((word) => letter.every((char) => !word.includes(char)));

console.log(resultArr);

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.