1

I'm working on a CSV uploader that uses PapaParse as it's CSV parser. For my CSV I would like my first column to act as my header for the parsed data as opposed to the first row. In order to get the expected outcome, I've been having to manually transpose the CSV in the editor before uploading.

The reason for this is that my users find it much easier to edit the CSV when the headers are in the first column and not the first row. Is there a way I can do this in PapaParse (or even JavaScript outside of PapaParse)?

if (file != null) {
    Papa.parse(file, {
        header: true,
        complete: function (results, file) {
            console.log("Parsing complete: ", results, file);
        }
    });
}
jmona789
  • 2,711
  • 6
  • 24
  • 57
Dylan
  • 697
  • 1
  • 9
  • 27

1 Answers1

1

I would suggest to parse the array with PapaParse and then perform transpose over the result with JS.

Using this method: https://stackoverflow.com/a/4492703/1625793

So it would look like that transpose(result.data)

-- Update --

const transposed = transpose(result.data)
const headers = transposed.shift();
const res = transposed.map(row => row.reduce((acc, col, ind) => {acc[headers[ind]] = col; return acc}, {}))
Community
  • 1
  • 1
Dmitrijs Balcers
  • 157
  • 1
  • 12
  • I was looking into that earlier but the issue comes when I want to use headers. When PapaParse outputs the results, the headers not correct so doing the transposition wouldn't resolve that issue. – Dylan Nov 24 '16 at 00:09
  • Can you give an example with what input (csv), what output you want to receive JSON? – Dmitrijs Balcers Nov 24 '16 at 00:11
  • Thank you very much! This worked great! I had to change a couple things on my end such as setting 'header: true' to 'header: false' to get it to work properly. – Dylan Nov 24 '16 at 06:44