gpt4 book ai didi

javascript - 如何使用 JavaScript 将对象转换为 CSV?

转载 作者:数据小太阳 更新时间:2023-10-29 04:10:37 25 4
gpt4 key购买 nike

我想将此对象转换为 CSV 文件。列名应该是键,这是一小块数组。最后一个数组将只是其中一种(键),所有其他数组将具有相同的键但具有不同的值。

[{
Comment: "Good",
Experince Months: "4",
Experince Years: "4",
Score: "3",
Subject: "CPP",
Topic: "Scripting (mention details)"
},
{
Comment: "Excilent",
Experince Months: "6",
Experince Years: "6",
Score: "6",
Subject: "CSharp",
Topic: "WPF"
},
{
Anything else worth highlighting: "Web Specialist",
Result: "Selected",
Total Business Analysis Experience: false,
Total Project Management Experience: false,
Total Score: 75,
Total Server Side Development Experience: true,
Total Server Side Support Experience: true,
Total UI Development Experience: true,
Total UI Support Experience: true
}]

最佳答案

这是 TSV 的简单实现(对于 csv,请参阅此答案的评论):

// Returns a csv from an array of objects with
// values separated by tabs and rows separated by newlines
function CSV(array) {
// Use first element to choose the keys and the order
var keys = Object.keys(array[0]);

// Build header
var result = keys.join("\t") + "\n";

// Add the rows
array.forEach(function(obj){
result += keys.map(k => obj[k]).join("\t") + "\n";
});

return result;
}

关于javascript - 如何使用 JavaScript 将对象转换为 CSV?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22784802/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com