Himas Rafeek
Posted on: 2 years ago

Javascript array To CSV Function

This snippet converts the elements that don’t have commas or double quotes to strings with comma-separated values.


                                          const arrayToCSV = (arr, delimiter = ',') =>
  arr.map(v => v.map(x => `"${x}"`).join(delimiter)).join('\n');
  
arrayToCSV([['a', 'b'], ['c', 'd']]); // '"a","b"\n"c","d"'
arrayToCSV([['a', 'b'], ['c', 'd']], ';'); // '"a";"b"\n"c";"d"'