How to export JavaScript array info to csv (on client side)?

前端 未结 29 1683
没有蜡笔的小新
没有蜡笔的小新 2020-11-21 21:55

I know there are lot of questions of this nature but I need to do this using JavaScript. I am using Dojo 1.8 and have all the attribute info in array, which loo

29条回答
  •  长情又很酷
    2020-11-21 22:50

    This is a modified answer based on the accepted answer wherein the data would be coming from JSON.

                JSON Data Ouptut:
                 0 :{emails: "SAMPLE Co., peter@samplecompany.com"}, 1:{emails: "Another CO. , ronald@another.com"}
    
    
                JS:
                $.getJSON('yourlink_goes_here', { if_you_have_parameters}, function(data) {
                var csvContent = "data:text/csv;charset=utf-8,";
                var dataString = '';
                 $.each(data, function(k, v) {
                    dataString += v.emails + "\n";
                 });
    
                csvContent += dataString;
    
                var encodedUri = encodeURI(csvContent);
                var link = document.createElement("a");
                link.setAttribute("href", encodedUri);
                link.setAttribute("download", "your_filename.csv");
                document.body.appendChild(link); // Required for FF
    
                link.click();
            });
    

提交回复
热议问题