Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

headers order isn't respected when data is an array of objects #58

Open
nicogreenarry opened this issue Nov 18, 2023 · 0 comments
Open

Comments

@nicogreenarry
Copy link

In the following case:

csvDownload({
  headers: ["first_column", "second_column"],
  data: [{ second_column: 2, first_column: 1 }],
  delimiter: ",",
});

...the output will actually be this, which is incorrect:

first_column,second_column
2,1

I haven't looked at the source code, but I imagine what's happening is that if data is an object, the library just serializes the object without regard for headers. But ideally it should read properties from each row object in the order of headers.

It's easy enough to fix in my own code, like this:

const headers = ["first_column", "second_column"]
const data = [{ second_column: 2, first_column: 1 }]
csvDownload({
  headers,
  data: data.map(row => headers.map(columnId => row[columnId])),
  delimiter: ",",
});

But ideally your library could do that so we don't have to :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant