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

107
Vistas
2 choices of copying multidimentional array data to clipboard in javascript

I would like to be able to have 2 buttons in my admin panel. One to copy my text as "pure text". And one button to copy my text while adding extra html tags( AND

). I do a lot of copy and paste for my work, and this would really help me work a bit faster.

Let's say you have this array:

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'}]

In this array, I would like to copy /paste the content(from the above array) like this to a text file:

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

(The <br> cannot be visible on the text file...it needs to be a blank space that works in a .txt format, could this " " works?)

The second button should also copy the text from the array but surrounds the body and content with the below tags:

<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>

At the moment, I am here:

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");
    })
}

I am not sure if I am going in the right direction.

Thanks for your help

about 4 years ago · Santiago Gelvez
1 Respuestas
Responde la pregunta

0

You will need to have 2 functions one for each button.

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 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