Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

166
Vistas
Javascript encuentra una oración coincidente con palabras predefinidas

Hola chicos necesito ayuda para descifrar una expresión. tengo frases como

 const phrases = [ "This is dummy sentences of example 05", "It needs help of your example 2022", "This dummy datas will be used for any example of matched needs " ] var target = "example dummy datas 01" expectedOutput = "This dummy datas will be used for any example of matched needs "

Como puede ver arriba, las tres palabras del objetivo coinciden con la última oración. El propósito es encontrar oraciones que incluyan la mayoría de las palabras coincidentes.

Me gustó

 const phrases = [ "This is dummy sentences of example 05", "It needs help of your example 2022", "This dummy datas will be used for any example of matched needs " ] var target = "example dummy datas 01" target = target.split(' ') const expectedOutput = phrases.filter(sentence => { return target.every(word => { return sentence.indexOf(word) !== -1 }) }) console.log(expectedOutput)

pero no da nada

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Utilice .filter() , .some( .some() , .includes() y .sort() como se explica a continuación.

 const phrases = [ "This is dummy sentences of example 05", "It needs help of your example 2022", "This dummy datas will be used for any example of matched needs " ] var target = "example dummy datas 01" target = target.split(' '); const expectedOutput = phrases //Find if any phrases contain at least one target word .filter( phrase => target.some(word => phrase.includes(word)) ) //Now sort the phrases in descending order of how many words matched .sort( (a,b) => target.filter(word => b.includes(word)).length - target.filter(word => a.includes(word)).length ) //The first phrase matched the most words or at least the first and second matched the most words, otherwise return '' [0] || ''; console.log( expectedOutput ); //Output: "This dummy datas will be used for any example of matched needs "

about 4 years ago · Juan Pablo Isaza Denunciar

0

Debe filtrar los elementos de la cadena de target que son numéricos:

 target = target.split(' ').filter(w => isNaN(w))

Esto funciona de la siguiente manera:

 const phrases = [ "This is dummy sentences of example 05", "It needs help of your example 2022", "This dummy datas will be used for any example of matched needs " ] var target = "example dummy datas 01" target = target.split(' ').filter(w => isNaN(w)) const expectedOutput = phrases.filter(sentence => { return target.every(word => { return sentence.indexOf(word) !== -1 }) }) console.log(expectedOutput)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda