Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

87
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda