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

85
Views
Buscar a través de una serie de cadenas para encontrar una de las palabras que coincidan

Estoy tratando de permitir que mi usuario ingrese un término de búsqueda y luego devuelva las cadenas en la matriz que coinciden con todos los nombres que ingresaron. Entonces, si escribieron clinton aquí, encontraría a todos los clinton, pero si buscaron hillary clinton, omitiendo el segundo nombre de rodham, devolvería hillary pero no bill o 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

Suponiendo que sus términos de búsqueda siempre estarán separados por un solo espacio, puede hacer algo como esto:

 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

Puede utilizar esta función para buscar.

 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!