用不同键从对象创建CSV
我有一系列对象。他们的某些键是相同的,但其中一些仅存在于其中一个对象中:
const data =[
{id:123, name:foo, number:25},
{id:124, name:boo, number:35, blue:8},
{id:125, name:soo, number:40, red:10}
]
我想将数据转换为CSV,以便标题将包含所有键。如果对象中不存在键的值应该为null,请参见下面的示例:
123 | foo | 25 | null | ID名称编号蓝色 |
---|---|---|---|---|
null | 124 | boo | 35 | 8 |
null | 125 | SOO | 40 | null |
10 | 我 | 能够 | 在 | 几个 |
循环中实现这一目标,但是代码很丑陋:)
谢谢!
I have an array of objects. Some of their keys are identical but some of them exist only in one of the object:
const data =[
{id:123, name:foo, number:25},
{id:124, name:boo, number:35, blue:8},
{id:125, name:soo, number:40, red:10}
]
I want to convert my data into a CSV, so that the headers will include all keys. if key doesn't exists in the object its value should be null , see example below:
id | name | number | blue | red |
---|---|---|---|---|
123 | foo | 25 | null | null |
124 | boo | 35 | 8 | null |
125 | soo | 40 | null | 10 |
I was able to achieve this with few loops , but the code is ugly :)
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
检查此解决方案是否适用于您的问题。
从Javascript对象
如果您对此过程有任何疑问,请在此处回复。我会尽力帮助
Check if this solution applies to your problem.
Generate a csv file from a javascript array of objects
If you have any doubts in the process, just reply here. I'll try to help