ITADN

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

#58Closednicogreenarry 创建于 2023-11-18
N
nicogreenarrycommented
In the following case: ```javascript csvDownload({ headers: ["first_column", "second_column"], data: [{ second_column: 2, first_column: 1 }], delimiter: ",", }); ``` ...the output will actually be this, which is incorrect: ```csv 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 :)
关闭于 2024-08-20 1 条评论