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

198
Views
¿Cómo acortar el texto en las primeras 50 palabras y volver a ponerlo en la función de detalle Javascript?

Quiero acortar el texto a los primeros 50 sin romper el texto. Parte del texto puede o no tener una etiqueta html, ya que estoy usando summernote.code para enviar el texto de entrada. Y ahora quiero recuperar el texto pero abreviar las primeras 50 palabras, ¿cómo hago eso?

Obtendré los datos primero y los asignaré con la nueva variable cutDesc = e.description; luego paso la variable a la función acortar. Después de haberlo acortado, lo almaceno en la variable newDescription y lo llamo nuevamente a la función axios get. Pero no está funcionando.

ejemplo e.description se verá así.

 <p><strong style="margin: 0px; padding: 0px; font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;">Lorem Ipsum</strong> <span style="font-family: &quot;Open Sans&quot;, Arial, sans-serif; text-align: justify;"> &nbsp;is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.</span></p>

Esto es lo que he intentado y no funciona, ¿cómo soluciono esto?

 let description = ''; let newDescription = ''; axios.get(`${url}info?id=${id}`) .then(function(res) { if (res.data.status == 'success') { $("#info_list").html(res.data.result.map((e) => { cutDesc = e.description; return `<tr class="tb-item"> <td class=""> ${count++}<br> </td> <td class="tb-data"> <span class="tb-data"> <small>${newDescription}</small> </span> </td> </tr>`; })); } } function shorten(cutDesc, max) { return cutDesc && cutDesc.length > max ? cutDesc.slice(0, max).split(' ').slice(0, -1).join(' ') : cutDesc } newDescription= shorten(cutDesc,50);
about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

en su función, puede eliminar las etiquetas primero, luego tomar las primeras 50 palabras así

 var shorten = (cutDesc, max) => { const cleanTextArr = cutDesc.replace(/(<([^>]+)>)/gi, "").split(' '); const firstFifty = cleanTextArr.splice(0, max).join(' '); console.log(firstFifty); return firstFifty }

y llame a esta función dentro de su promesa then de cutDesc = e.description; como esto

 axios.get(`${url}info?id=${id}`) .then(function(res) { if (res.data.status == 'success') { $("#info_list").html(res.data.result.map((e) => { cutDesc = e.description; newDescription = shorten(cutDesc, 50); return `<tr class="tb-item"> <td class=""> ${count++}<br> </td> <td class="tb-data"> <span class="tb-data"> <small>${newDescription}</small> </span> </td> </tr>`; })); } }

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!