Search code examples
javascriptreactjsarraystranspose

Tranpose react-csv list


I am using the react-csv package to download a list of values stored in an array in a React app I am making like so:

<CSVLink data={[applicableCriteria]}>Download</CSVLink>

When I click Download and view the contents, I get a long row of all the list contents. Rather than being one long row, I would prefer one long column with the contents from my array. Is there a good way to transpose the 1-D array to accomplish this goal? Thank you.

applicableCriteria is an array of numbers, e.g., ["1.1.1", "1.1.2", "1.1.3", ... ].


Solution

  • it is because according to the documentation when you pass in an array of arrays
    i.e [["1.1.1", "1.1.2", "1.1.3", ... ]] each array in the main array is one row.
    Therefore you will need to convert to [["1.1.1"],["1.1.2"],["1.1.3"], ... ] format. Now there are multiple rows with 1 column.

    <CSVLink data={applicableCriteria.map(x => [x])}>Download</CSVLink>
    

    Edit react-csv (forked)