Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

javascript array to csv string

var colors = ["red", "blue", "green"];
var colorsString = colors.join(","); //"red,blue,green"
Comment

js array to csv

const arrayToCSV = (arr, delimiter = ',') =>
  arr.map(v => v.map(x => `"${x}"`).join(delimiter)).join('
');
Comment

convert array to csv javascript

var objectToCSVRow = function(dataObject) {
    var dataArray = new Array;
    for (var o in dataObject) {
        var innerValue = dataObject[o]===null?'':dataObject[o].toString();
        var result = innerValue.replace(/"/g, '""');
        result = '"' + result + '"';
        dataArray.push(result);
    }
    return dataArray.join(' ') + '
';
}

var exportToCSV = function(arrayOfObjects) {

    if (!arrayOfObjects.length) {
        return;
    }

    var csvContent = "data:text/csv;charset=utf-8,";

    // headers
    csvContent += objectToCSVRow(Object.keys(arrayOfObjects[0]));

    arrayOfObjects.forEach(function(item){
        csvContent += objectToCSVRow(item);
    }); 

    var encodedUri = encodeURI(csvContent);
    var link = document.createElement("a");
    link.setAttribute("href", encodedUri);
    link.setAttribute("download", "customers.csv");
    document.body.appendChild(link); // Required for FF
    link.click();
    document.body.removeChild(link); 
}
Comment

javascript array to csv

/** Convert a 2D array into a CSV string
 */
function arrayToCsv(data){
  return data.map(row =>
    row
    .map(String)  // convert every value to String
    .map(v => v.replaceAll('"', '""'))  // escape double colons
    .map(v => `"${v}"`)  // quote it
    .join(',')  // comma-separated
  ).join('
');  // rows starting on new lines
}

/** Download contents as a file
 * Source: https://stackoverflow.com/questions/14964035/how-to-export-javascript-array-info-to-csv-on-client-side
 */
function downloadBlob(content, filename, contentType) {
  // Create a blob
  var blob = new Blob([content], { type: contentType });
  var url = URL.createObjectURL(blob);

  // Create a link to download it
  var pom = document.createElement('a');
  pom.href = url;
  pom.setAttribute('download', filename);
  pom.click();
}

downloadBlob(csv, 'export.csv', 'text/csv;charset=utf-8;')
Comment

PREVIOUS NEXT
Code Example
Javascript :: npm react router dom 
Javascript :: regex password validation 
Javascript :: communication with service worker 
Javascript :: angular ng build Maximum call stack size exceeded 
Javascript :: initialize json array 
Javascript :: loop json object android java 
Javascript :: datatable after render event 
Javascript :: jquery datepicker change date format 
Javascript :: js animate scroll to the top of the page 
Javascript :: jquery selected option 
Javascript :: jquery find parent 
Javascript :: js nwe date today 
Javascript :: js rectangle collision 
Javascript :: jquery radio button checked event 
Javascript :: how to set content length of an mp3 stream in nodejs 
Javascript :: vue electron min width 
Javascript :: how to get the data attached with an element in javascript 
Javascript :: make button disabled 
Javascript :: javascript get div x y position 
Javascript :: button click javascript 
Javascript :: how to close another browser tab with javascript 
Javascript :: javascript select option value onchange 
Javascript :: how to write tuples in elixir 
Javascript :: sendgrid bulk hide each other on the email 
Javascript :: current time in javascript 
Javascript :: jquery on scroll down 
Javascript :: nodejs powershell process env 
Javascript :: js remove end comma 
Javascript :: codewars js Number of People in the Bus 
Javascript :: array notation in javascript 
ADD CONTENT
Topic
Content
Source link
Name
4+3 =