gpt4 book ai didi

javascript - JSON 到 CSV : How to create a table column for all fields in a collection when using Papa. 解解析()?

转载 作者:行者123 更新时间:2023-11-28 05:58:32 24 4
gpt4 key购买 nike

我使用 Papa Parse 4。当我使用 Papa.unparse(collection) 时,它似乎从 JSON 集合中的第一个文档创建结果表的列。我希望我的集合中所有可能的字段都显示在结果表中。

一个例子:

{ "name": "Ross" },
{ "name": "Bob", "age": 63 }

创建一个只有一列的表:“name”:

name
Ross
Bob

我想要:

name    age
Ross
Bob 33

如何让 Papa Parse 使用“最大”的 JSON 对象来创建列?

最佳答案

Papa 解析仅考虑第一个 JSON 来生成 header 。检查this issue了解更多信息。但是,您可以使用以下方法将所有 JSON 对象中的字段包含在最终的 CSV 导出中:

Papa.unparse({
data: [ ... ], // JSON array
fields: [ ... ] // Fields to include in CSV eg. ['name', 'age']
});

关于javascript - JSON 到 CSV : How to create a table column for all fields in a collection when using Papa. 解解析()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37463764/

24 4 0