I got a string, and an array of strings. Examples of the string are:
let example1 = "_a_t_";
let example2 = "N_l_:t___";
let example3 = "B__z_l";
What we can see is that the string is Z characters long and got a random amount of characters that is not _. The string will never be only _ nor will it ever be without an underscore.
let array = ["Walts", "Grate", "Ralts", "Names"];
If we use the example1 string and the array above, I want to filter the array so the outcome becomes:
let newArray = ["Walts", "Ralts"];
Basically, of the known characters I want to use them and their position to filter the array. Currently I've found how to find the first character and use it to filter the array. This is how that was done:
let example1 = "_a_t_";
let array = ["Walts", "Grate", "Ralts", "Names"];
let charIndex = example1.match("[a-zA-Z]").index;
/*Currently using the RegEx [a-zA-Z], but this should be changed to include all characters
besides underscores but I don't know what to put in between the square brackets*/
let firstChar = example1.charAt(charIndex);
let filteredArray = array.filter(function (element) {
return element.charAt(charIndex) === firstChar;
});
console.log(filteredArray); //this returns ["Walts", "Ralts", "Names"]
It is here I am stuck. I got no idea how to do this if I got multiple revealed characters in the string. After pondering a bit, the logical thing to me would be to somehow count all the characters that's not an underscore, then using that, make a loop. The loop would find each character and its index, then filter the array. The loop would finish when the array is completely filtered. Using the above array and example1
string, the wished goal would be to get ["Walts", "Ralts"]
.
I think that the problem is thoroughly explained, and that the end goal is clear. This is my first ever post on Stack Overflow so I'm very excited.