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

108
Views
2 opciones de copiar datos de matriz multidimensional al portapapeles en javascript

Me gustaría poder tener 2 botones en mi panel de administración. Uno para copiar mi texto como "texto puro". Y un botón para copiar mi texto mientras agrego etiquetas html adicionales (Y

). Copio y pego mucho en mi trabajo, y esto realmente me ayudaría a trabajar un poco más rápido.

Digamos que tienes esta matriz:

 let array = [{'head': 1, 'body': 'This is the body 1', 'content': 'This is the content 1'},{'head': 2, 'body': 'This is the body 2', 'content': 'This is the content 2'},{'head': 3, 'body': 'This is the body 3', 'content': 'This is the content 3'}]

En esta matriz, me gustaría copiar/pegar el contenido (de la matriz anterior) de esta manera en un archivo de texto:

 This is the body 1 This is the content 1   This is the body 2 This is the content 2   This is the body 3 This is the content 3

(El <br> no puede ser visible en el archivo de texto... debe ser un espacio en blanco que funcione en formato .txt, ¿podría funcionar este " "?)

El segundo botón también debe copiar el texto de la matriz, pero rodea el cuerpo y el contenido con las siguientes etiquetas:

 <h2>This is the body 1</h2> <p>This is the content 1</p> <br> <h2>This is the body 2</h2> <p>This is the content 2</p> <br> <h2>This is the body 3</h2> <p>This is the content 3</p>

En este momento, estoy aquí:

 function copyToClipboard(){ let array = [{'head': 1, 'body': 'This is the body 1', 'content': 'This is the content 1'},{'head': 2, 'body': 'This is the body 2', 'content': 'This is the content 2'},{'head': 3, 'body': 'This is the body 3', 'content': 'This is the content 3'}] //Copy(clipboard) Text only var text = ""; //For Text only for (let i = 0; i < array.length; i++) { text += array[i]['body'] + "&nbsp;" + array[i]['content'] + "&nbsp;"; } //Copy(clipboard) Text with html tags for (let i = 0; i < array.length; i++) { text += "<h2>" + array[i]['body'] + "</h2>" + "<p>" + array[i]['content'] + "</p>"; } navigator.clipboard.writeText(text).then(() => { alert("Copied to clipboard"); console.log("Copied to clipboard"); }) }

No estoy seguro de si voy en la dirección correcta.

Gracias por tu ayuda

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Necesitarás tener 2 funciones una para cada botón.

 let array = [{'head': 1, 'body': 'This is the body 1', 'content': 'This is the content 1'},{'head': 2, 'body': 'This is the body 2', 'content': 'This is the content 2'},{'head': 3, 'body': 'This is the body 3', 'content': 'This is the content 3'}] function copyAsText(){ //Copy(clipboard) Text only var text = ""; //For Text only for (let i = 0; i < array.length; i++) { text += array[i]['body'] + "\n" + array[i]['content'] + "\n"; } navigator.clipboard.writeText(text).then(() => { console.log("Copied to clipboard"); }) } function copyAsHTML(){ var text = ""; //Copy(clipboard) Text with html tags for (let i = 0; i < array.length; i++) { text += "<h2>" + array[i]['body'] + "</h2>" + "<br>" + "<p>" + array[i]['content'] + "</p><br>"; } navigator.clipboard.writeText(text).then(() => { console.log("Copied to clipboard"); }) }
about 4 years ago · Santiago Gelvez 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!