0

I'm using https://www.npmjs.com/package/react-csv library to export some data by generating a csv file. The above mentioned library supports the following data format

const csvData = [
  ["firstname", "lastname", "email"],
  ["Ahmed", "Tomi", "[email protected]"],
  ["Raed", "Labes", "[email protected]"],
  ["Yezzi", "Min l3b", "[email protected]"]
];

The data i want to export is getting from a API response. I'm mapping my data array to create a data structure as above.

    const csvData = [
      ["A", "B", "C", "D", "E", "F", "G", "H"],
      transaction.transactionData && transaction.transactionData.map((item, key) => {
           return (
               [item.A, item.B, item.C, item.D, item.E, item.F, item.G, item.H]
           )
      })
     ]

But this won't output the structure i want (the one i showed you in the beginning). How can i map my data so it takes the required structure.

i console logged csvData and it looks like this

enter image description here

1 Answer 1

1

your code return another result like this:

[["A", "B", "C", "D", "E", "F", "G", "H"],[[...],[...]]]

this code is work correctly :

const output = []
output.push(["A", "B", "C", "D", "E", "F", "G", "H"])
transaction.transactionData && transaction.transactionData.forEach(item => {
           output.push (
               [item.A, item.B, item.C, item.D, item.E, item.F, item.G, item.H]
           )
      })

We must define a variable as an array for our structure. after that push our rows in our variable.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.