How can I export a JavaScript array to a CSV file directly on the client side?

I have data in an array format like ``[[“name1”, “city_name1”, …]`, “name2”, “city_name2”, …]] and want to download it as a CSV using JavaScript (working with Dojo 1.8).

What’s the best way to implement this client-side CSV export?

A neat way to export your array as CSV on the client side is to build the CSV content as a string by joining each row with commas and each line with \n.

Then you create a Blob from that string with MIME type ‘text/csv’ and trigger a download by creating an anchor element with a download attribute. It works well in most modern browsers, and you don’t need any extra libraries.

If you’re working with Dojo 1.8, you can still do this pretty easily by manually converting your array to CSV string format - just handle commas and quotes carefully and then use a Blob and URL.createObjectURL to create a downloadable link.

Then programmatically click it to start the download. It’s straightforward and keeps everything client-side.

The simplest approach is to loop through your 2D array, convert each inner array into a CSV line, and concatenate them.

Once you have the CSV text, use the Blob API to create a CSV file on the fly, then generate a URL and simulate a click on a hidden link with the download attribute. This way, your users get a file download without any server interaction.