I'm given a CSV that contains many rows. This CSV may or may not contain a header. E.g.
Column Name 1,Column Name 2
data1,data2
data3,data4
data5,data6
...
My goal is to extract the leftmost column into an array, which I can do by doing this
const column = csv
.split('\n')
.filter(r => r)
.map(r => r.split(',')[column]?.trim())
.slice(1);
This would return
['data1', 'data3', 'data5']
My issue is that this is assuming that the CSV has a header - I slice(1) the header out of the solution. If I were given a CSV without a header, I would accidentally omit the 0th row.
Is there a way to check if headers exist, or a better solution to this problem in general?