DTmann DTmann - 8 months ago 83
jQuery Question

Parse a transposed CSV with headers using PapaParse

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);

Answer Source

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

Using this method: http://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}, {}))