Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

82
Views
Searching through an array of strings to find one of the words matching

I'm trying to allow my user to enter a search term and then return the strings in the array that match all names they entered. So if they typed clinton here, it would find all the clintons, but if they searched hillary clinton, leaving out the rodham middle name, it would return hillary but not bill or chelsea.

const array = ['hillary rodham clinton', 'bill clinton', 'chealsea clinton', 'louise penny', 'amanda litman']

const searchTerm1 = 'hillary clinton' // should return hillary rodham clinton
const searchTerm2 = 'clinton' // should return hillary rodham clinton, bill clinton, chelsea clinton
const searchTerm3 = 'hillary' // should return hillary rodham clinton

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Assuming your search terms will always be separated by a single space, you can do something like this:

const array = ['hillary rodham clinton', 'bill clinton', 'chealsea clinton', 'louise penny', 'amanda litman']

const searchTerm1 = 'hillary clinton' // should return hillary rodham clinton
const searchTerm2 = 'clinton' // should return hillary rodham clinton, bill clinton, chelsea clinton
const searchTerm3 = 'hillary' // should return hillary rodham clinton

let find = (term) => array.filter(item => term.split(' ').every(r => item.split(' ').includes(r)))

console.log(find(searchTerm1))
console.log(find(searchTerm2))
console.log(find(searchTerm3))

about 4 years ago · Juan Pablo Isaza Report

0

You can use this function to search.

function search(searchTerm, array) {
        const words = searchTerm.split(" ");
        let tmpArray = array;
        for (let i = 0; i < words.length; i++) {
            tmpArray = tmpArray.filter(obj => obj.indexOf(words[i]) >= 0);
        }
            
        return tmpArray;
}

const newArray1 = search(searchTerm1, array);
const newArray2 = search(searchTerm2, array);
const newArray3 = search(searchTerm3, array);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!