I am looking for a way to search for a name within an array, including situations where the search input (of multiple words: first name, surname) may be reversed.
The array would look like this with a series of names.
const names = ['Alan Hope', 'Greg Day', 'Alan Peters']
The search input could be as follows 'peter Al'
What would the code look like to achieve this. This is what I have so far and I know it is totally wrong.
const studentNames = ['Alan Hope', 'Greg Day', 'Alan Peters']
function search () {
const bankingSheet = ss.getSheetByName('Banking')
const searchInput = 'Hope Al'
const searchWords = searchInput.split(/\s+/)
const filtered = studentNames.filter(function(name) {
searchWords.every(function(word) {
return name.toString().toLowerCase().indexOf(word) !== -1
})
})
Logger.log(filtered)
}
From what I gather I need to first split the search input into the constituent words. I then need to filter through the names array. For each name in the array I need to check if all search words appear in some way in the name. I think this may involve the every method.
For each name, if the return value is true that is what I need to return.
Is this thinking correct?
Thank you in advance! This is really hurting my head at the moment!