0

How to export JavaScript array into to CSV?

const rows = [
    ["name1", "city1", "some other info"],
    ["name2", "city2", "more info"]
];

let csvContent = "data:text/csv;charset=utf-8," 
    + rows.map(e => e.join(",")).join("\n");

var button = document.querySelector('button');

button.addEventListener('click', () => {
  let encodedUri = encodeURI(csvContent);
window.open(encodedUri);
});
<pre>
const rows = [
    ["name1", "city1", "some other info"],
    ["name2", "city2", "more info"]
];
</pre>

<button>
  Download CSV
</button>
button {
  padding: 20px;
  font-size: 40px;
}
RUN
add comment
Suggest a different solution or add another example