Converting a collection of objects to CSV with keys as headers and values

If that is array of objects you can first get header and then values and create string from that.

const data = [ {timestamp: 1525879470,name: "testing",lastname: "testingdone"
}, {timestamp: 1525879470,name: "testing2",lastname: "testingdone2"}]

let csv = '';
let header = Object.keys(data[0]).join(',');
let values = data.map(o => Object.values(o).join(',')).join('\n');

csv += header + '\n' + values;
console.log(csv)

Tags:

Javascript

Csv