const rows = [
["Name", "City", "Info"],
["name1", "city1", "some other info"],
["name2", "city2", "more info"]
];
let csvContent = rows.map(e => e.join(",")).join("\n");
Similarly how can I implement the same on an Object array for example
const rows = [
{name:"FirstName", city:"City", info:"Info"},
{name:"LastName", city:"City", info:"Info"},
];
First you can Array#reduce it, saving the Object.keys as the first header row, then creating the array structure. Finally map all that just like your first example.
const rows2 = [{
name: "FirstName",
city: "City",
info: "Info"
},
{
name: "LastName",
city: "City",
info: "Info"
},
];
let csvContent2 = rows2.reduce((b, a, i) => {
if (i === 0) b.push(Object.keys(a))
return b.concat([Object.values(a)]);
}, []).map(e => e.join(",")).join("\n")
console.log(csvContent2);