me-me
me-me

Reputation: 5819

Search through array of keywords to see if any characters match a users input

I've tested this and am getting true only when the last character is found. I want it to match as the user types.

So input is what I capture on input change event as the user types. If a input character matches any of the characters in the words array then it should return true for a match.

Example:

    const input = 'mem';
    const inputLower = input.toLowerCase();
    const words = ['member', 'support', 'life'];
    words.forEach(word => {
      const charList = word.split('');
      console.log('charList = ', charList);
      const isMatch = charList.every(char => { 
        console.log('char = ', char, '  inputLower = ', inputLower);
        return inputLower.includes(char);
      });
      console.log('isMatch = ', isMatch);
    });

Upvotes: 0

Views: 60

Answers (1)

Nico Diz
Nico Diz

Reputation: 1504

Try this:

const input = 'mem';
const inputLower = input.toLowerCase();
const words = ['member', 'support', 'life'];
words.forEach(word => {
    const isMatch = word.includes(input);
    console.log({ input, currentWord: word, isMatch });
});

Upvotes: 1

Related Questions