用不同键从对象创建CSV

发布于 2025-02-06 16:54:33 字数 1222 浏览 0 评论 0原文

我有一系列对象。他们的某些键是相同的,但其中一些仅存在于其中一个对象中:

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,请参见下面的示例:

123foo25nullID名称编号蓝色
null124boo358
null125SOO40null
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:

idnamenumberbluered
123foo25nullnull
124boo358null
125soo40null10

I was able to achieve this with few loops , but the code is ugly :)

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

执笏见 2025-02-13 16:54:33

检查此解决方案是否适用于您的问题。

从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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文