Filter array by letter

23,172

Solution 1

You can define your own filter function :

function filter(names, index, letter) {
    var filteredNames = names.filter(function(word) {
       return word.charAt(index) === letter;
    });
    return filteredNames;
}

Solution 2

A regexp will be more flexible I guess

var filteredNames = names.filter(function(word) {
    return /^A/.test(word);
});

A generic way to use it

function filterMatches(words, regexp) {
    return words.filter(function (word) {
         return regexp.test(word);
    });
}

filterMatches(words, /^A/); // for letter A, index 0
filterMatches(words, /^.{3}B/); // for letter B, index 4

Solution 3

why not create a function to do just what you want? From your code it be like this:

function filterName(arrayOfNames, index, letter) {
    var filteredNames = arrayOfNames.filter(function(word) {
     return word[index] === letter;
    });
    return filteredNames 
}

So you can just pass on the array, index and letter to it:

console.log(filterName(arrayOfNames, 3, 'x'));

Solution 4

You can create your own custom function

var array =["Anna", "Bob", "Charles", "Daniel", "Allison", "Beatrice", "Cindy", "Fiona"];
var matched_terms = [];
var search_term = "an";
search_term  = search_term.toLowerCase();
           array.forEach(item => {
                  if(item.toLowerCase().indexOf(search_term) !== -1 ){
                  console.log(item);
                  matched_terms.push( item ); 
      }

console.log(matched_terms);
})
Share:
23,172
MateBoy
Author by

MateBoy

I think CSS is a very underappreciated language. Yes, I am serious. Favorite font at the moment: Aperçu Least favorite font at the moment: Lobster

Updated on June 20, 2020

Comments

  • MateBoy
    MateBoy almost 4 years

    Pardon me if this has been asked but I searched and didn't find the specific implementation of my problem.

    Anyway, I'm currently learning high-order functions in JavaScript and I'm at the array.prototype.filter function. I understand its purpose (as its name so conveniently implies) but I'm having trouble implementing this:

    So, say I have an array of names, like this:

    var names = ["Anna", "Bob", "Charles", "Daniel",
             "Allison", "Beatrice", "Cindy", "Fiona"];
    

    And then I want to, say, filter that array by all entries that start with the letter "A". I'm aware of the fact that I could do this:

    var filteredNames = names.filter(function(word) {
        return word[0] === "A";
    });
    

    And that would work just fine. But say I want to be less explicit and make it more adaptable to more situations. Say I want to program the filtering so that I can say "return only the entries that have the letter "x" at index [y]", for example "return only the entries that have the letter "F" at index[3].

    How can I achieve that?

  • Vibhesh Kaul
    Vibhesh Kaul over 8 years
    "return only the entries that have the letter "x" at index [y]", please read the question carefully.
  • axelduch
    axelduch over 8 years
    I read the question carefully, and this is still my answer, because it covers the question's concerns.