I have a function which writes values to a csv file from an array in some response, but before that I need to provide headers with the field name in the first line for each tab so I have written the headers in using csv += '/t header 1',csv += '/t header 2' and so on.
Here is my block of code
function exportToCsv(fName, rows) {
var csv = 'branch';
csv += '\t customerId';
csv += '\t customerName';
csv += '\t LOAN ID/UT unique ID';
csv += '\n';
for (var i = 0; i < rows.length; i++) {
var row = Object.values(rows[i]);
for (var j = 0; j < row.length; j++) {
var val = '';
val = row[j] === null ? '' : row[j].toString();
if (j > 0)
csv += '\t';
csv += val;
}
csv += '\n';
}
}
Is there any efficient way to write those five lines in above function? The current code is working but I'm looking for a more efficient way to replace these lines.
Also note I have just mentioned a few header names here but I actually have 20 - 30 headers fields.
Please share your thoughts.
rowsin the question would also be of assistance.