0

I'm able to generate a CSV file from a single array using the following JS code:

var token_array = ["123ad4", "35cv34", "345cv5"];

// this will download a csv of the tokens
        var csv = "";
        for (let row of token_array) {
          for (let col of row) { csv += col}
            csv += "\r\n";
        }
  
        // (C) CREATE BLOB OBJECT
        var myBlob = new Blob([csv], {type: "text/csv"});
  
        // (D) CREATE DOWNLOAD LINK
        var url = window.URL.createObjectURL(myBlob);
        var anchor = document.createElement("a");
        anchor.href = url;
        //this will output to the default download location - the 'downloads' folder.  
        anchor.download = "tokens.csv";
  
        // (E) "FORCE DOWNLOAD"
        // NOTE: MAY NOT ALWAYS WORK DUE TO BROWSER SECURITY
        // BETTER TO LET USERS CLICK ON THEIR OWN
       anchor.click();
       window.URL.revokeObjectURL(url);
       anchor.remove();

    }).fail(function(){
        console.log("An error has occurred.");
    });

Which outputs a csv that looks like this:

123ad4
35cv34
345cv5

But let's say I have two more arrays, which I want to add as additional columns to the csv:

var name_array = ["Tim", "Jon", "Albert"];
var sex_array = ["M", "M", "M"];

Desired output:

Tim        M     123ad4
Jon        M     35cv34
Albert     M     345cv5

How might I achieve this using similar JS code to what I have above?

halfer
  • 19,824
  • 17
  • 99
  • 186
DiamondJoe12
  • 1,879
  • 7
  • 33
  • 81

0 Answers0