Home > Net >  Trying to match on multiple characters
Trying to match on multiple characters

Time:02-10

Have tried a variety of things but nothing is quite working...

I have the following code:

        correctList(id, e) {
          if (this.regex.test(e.key)) {
            this.correctLetters.push([e.key, id]);
          }

          this.correctLetters.forEach(
            (array) => {
              if (this.filteredWords.length > 1) {
                this.filteredList = this.filteredWords.filter((word) => word.indexOf(array[0]) === array[1]);
            } else {
                this.filteredList = this.FiveLetterWords.filter((word) => word.indexOf(array[0]) === array[1]);
            }
            this.setFilteredWords(this.filteredList);
            }
          );
        },

Where 'this.correctLetters[]' is:

[[e,0], [l,1], [d,2]...]

This is built up using inputs and the order they are typed is the index number ( the 0, 1, 2 in the arrays)

and

'this.filteredWords' is:

["cigar", "rebut", "sissy", "humph", "awake", "blush",  "focal", "evade", "naval", "serve", "heath", "dwarf", "model", "karma", "stink", "grade", "quiet", "bench", "abate", "feign", "major", "death", "fresh", "crust", "stool", "colon", "abase", "marry", "react", "batty", "pride", "floss", "helix", "croak", "staff", "paper", "unfed", "whelp", "trawl", "outdo", "adobe", "crazy", "sower", "repay", "digit", "crate", "cluck", "spike", "mimic", "elder".....]

There are many more, but not really relevant to list all for question

My problem is the filter... Take the Word "elder"... If I type "e", "l" , "d", then I will get the list filters by words starting with "e", then filtered by words starting "el" and then filtered but words staring "eld".But as soon as I type "elde", it filters the array to empty (which I understand is technically correct due to my filtering method currently) and then if I type "elder", I will get all words ending in "r".

So essentially I understand my problem is the indexOf(() part of things as it`s only matching the first instance of the letter in the word, but I want to filter on words where the letter === the index of that letter but I am not sure how to change things. So if I type E R then right ow, I get only ending in R words, I want any words ending ER, including any that START with E also, or have another E in there...

Hopefully this makes some sense, am going round in circles with this.... Somebody suggested regex, but my first few attempts didn`t help, but possibly doing it wrong...

EDIT: Sort of fixed it with:

correctList(id, e) {
    if (this.regex.test(e.key)) {
    this.correctLetters.push([e.key, id]);
    }

    this.correctLetters.forEach(
    (array) => {
        if (this.filteredWords.length > 1) {
        this.filteredList = this.filteredWords.filter(
            (word) => {
                const wordSplit = word.split('');
                return wordSplit[array[1]] === array[0];
            }
        );
    } else {
        this.filteredList = this.FiveLetterWords.filter(
            (word) => {
                const wordSplit = word.split('');
                return wordSplit[array[1]] === array[0];
            }
        );
    }
    console.log(this.filteredList);
    this.setFilteredWords(this.filteredList);
    }
    );

But not sure if there is a more elegant method...

CodePudding user response:

To get the set of words that have the given letters in the given positions, just do this...

filteredList = FiveLetterWords.filter(
    word => correctLetters.reduce(
        (prev, [letter, index]) => prev && (word[index] === letter),
        true
    )
);

CodePudding user response:

I may have completely missed the point here, but it looks like you're just trying to filter a list of words that contain what the user has typed/selected somewhere.... in which case its as easy as

const words = filteredWords.filter(x => x.indexOf(something) > -1);

An example of this can be seen below, (start typing er and you'll get any word with "er" in it somewhere):

const filteredWords = ["cigar", "rebut", "sissy", "humph", "awake", "blush",  "focal", "evade", "naval", "serve", "heath", "dwarf", "model", "karma", "stink", "grade", "quiet", "bench", "abate", "feign", "major", "death", "fresh", "crust", "stool", "colon", "abase", "marry", "react", "batty", "pride", "floss", "helix", "croak", "staff", "paper", "unfed", "whelp", "trawl", "outdo", "adobe", "crazy", "sower", "repay", "digit", "crate", "cluck", "spike", "mimic", "elder"]

document.getElementById("entry").addEventListener("input", e => {
  const words = filteredWords.filter(x => x.indexOf(e.target.value)>-1);
  document.getElementById("output").value = words.join("\r\n")
})
<input id="entry" type="text">
<br>
<textarea id="output" rows="10"></textarea>

  • Related