Q:

Write a JavaScript program to converts a comma-separated values (CSV) string to a 2D array

0

Write a JavaScript program to converts a comma-separated values (CSV) string to a 2D array.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

//#Source https://bit.ly/2neWfJ2
const csv_to_array = (data, delimiter = ',', omitFirstRow = false) =>
  data
    .slice(omitFirstRow ? data.indexOf('\n') + 1 : 0)
    .split('\n')
    .map(v => v.split(delimiter));

console.log(csv_to_array('a,b\nc,d')); 
console.log(csv_to_array('a;b\nc;d', ';')); 
console.log(csv_to_array('head1,head2\na,b\nc,d', ',', true));

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now