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

106
Views
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 answers
Answer question

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