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

150
Views
Método de refactor que encadena javascript

Llamo a una función con un montón de métodos anidados, estoy usando herramientas de raspado web llamadas cheerio y quiero sacar el texto, así que necesito llamar a un montón de funciones. aquí está mi código de ejemplo.

 var text = $(el) .children() .first() .children() .first() .children() .first() .text() .replace(/\s\s+/g, " ");

Mi pregunta es, ¿cómo puedo simplificar mi función para obtener el mismo resultado sin encadenar mi función?

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

0

Una solución ingenua podría ser

 var text = $(">*:nth-child(1)".repeat(3), el).text().replace(/\s+/g, " ");

Para una buena solución necesitaríamos más contexto. Como se ve el marcado.

about 4 years ago · Juan Pablo Isaza Report

0

Puede usar el selector nth-child(1) para seleccionar el primer hijo.

Puedes usar así

 $("div div:nth-child(1) div:nth-child(1) ul:nth-child(1)").text()
about 4 years ago · Juan Pablo Isaza Report

0

usa un bucle

 let child = $(el).children().first() if (child) { while (child.children().first()) { child = child.children().first() } child.text().replace(/\s\s+/g, " "); }
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!