Node.js 将数组另存为任何格式的输出文件
我需要帮助了解如何保存数组的输出。我已尝试将输出附加到csv,但这会为数组中的每个项目提供一个空csv或一个附加有单词[object]的csv 以下是我的数组中的输出示例:Node.js 将数组另存为任何格式的输出文件,node.js,json,csv,Node.js,Json,Csv,我需要帮助了解如何保存数组的输出。我已尝试将输出附加到csv,但这会为数组中的每个项目提供一个空csv或一个附加有单词[object]的csv 以下是我的数组中的输出示例: { date: '2014-01-25', firstBoxerRating: [Array], firstBoxerWeight: 235.5, judges: [Array], links: [Object], location: 'Golden N
{
date: '2014-01-25',
firstBoxerRating: [Array],
firstBoxerWeight: 235.5,
judges: [Array],
links: [Object],
location: 'Golden Nugget Casino, Atlantic City',
metadata: '<a href="/en/judge/401833">Joseph Pasquale</a> 75-77 | <a href="/en/judge/401066">Tony Perez</a> 78-74 | <a href="/en/judge/401734">Barbara Perez</a> 78-74\n' +
'<br>',
numberOfRounds: [Array],
outcome: 'win',
rating: 20,
referee: [Object],
result: [Array],
secondBoxer: [Object],
secondBoxerLast6: [Array],
secondBoxerRating: [Array],
secondBoxerRecord: [Object],
secondBoxerWeight: 236.5,
titles: []
},
{
date: '2014-05-16',
firstBoxerRating: [Array],
firstBoxerWeight: 240.75,
judges: [Array],
links: [Object],
location: '2300 Arena, Philadelphia',
metadata: '<a href="/en/judge/402009">Pierre Benoist</a> 79-73 | <a href="/en/judge/401245">Lynne Carter</a> 78-74 | <a href="/en/judge/677642">Eric Dali</a> 79-73\n' +
'<br>',
numberOfRounds: [Array],
outcome: 'win',
rating: 20,
referee: [Object],
result: [Array],
secondBoxer: [Object],
secondBoxerLast6: [Array],
secondBoxerRating: [Array],
secondBoxerRecord: [Object],
secondBoxerWeight: 238,
titles: []
},
{
date: '2014-08-02',
firstBoxerRating: [Array],
firstBoxerWeight: 236.5,
judges: [Array],
links: [Object],
location: 'Revel Resort, Atlantic City',
metadata: ' time: 1:48\n' +
' | <a href="/en/judge/401683">Lindsey Page</a>\n' +
'<br>Williams down three times\n' +
'<br>',
numberOfRounds: [Array],
outcome: 'win',
rating: 20,
referee: [Object],
result: [Array],
secondBoxer: [Object],
secondBoxerLast6: [Array],
secondBoxerRating: [Array],
secondBoxerRecord: [Object],
secondBoxerWeight: 233,
titles: []
},
{
date: '2014-09-19',
firstBoxerRating: [Array],
firstBoxerWeight: 237,
judges: [Array],
links: [Object],
location: "Harrah's Philadelphia, Chester",
metadata: ' time: 1:34\n' +
' | <span>referee:</span> <a href="/en/referee/401364">Benjy Esteves Jr</a><span> | </span><a href="/en/judge/401043">Bernard Bruni</a> | <a href="/en/judge/400983">Larry Hazzard Jr</a> | <a href="/en/judge/402781">Alan Rubenstein</a>\n' +
'<br>',
numberOfRounds: [Array],
outcome: 'win',
rating: 20,
referee: [Object],
result: [Array],
secondBoxer: [Object],
secondBoxerLast6: [Array],
secondBoxerRating: [Array],
secondBoxerRecord: [Object],
secondBoxerWeight: 288,
titles: []
},
{
date: '2014-11-14',
firstBoxerRating: [Array],
firstBoxerWeight: 244,
judges: [Array],
links: [Object],
location: "Harrah's Philadelphia, Chester",
metadata: ' time: 2:28\n' +
' | <span>referee:</span> <a href="/en/referee/401364">Benjy Esteves Jr</a><span> | </span><a href="/en/judge/677642">Eric Dali</a> | <a href="/en/judge/400983">Larry Hazzard Jr</a> | <a href="/en/judge/671032">Mike Somma</a>\n' +
'<br>',
numberOfRounds: [Array],
outcome: 'win',
rating: 40,
referee: [Object],
result: [Array],
secondBoxer: [Object],
secondBoxerLast6: [Array],
secondBoxerRating: [Array],
secondBoxerRecord: [Object],
secondBoxerWeight: 209,
titles: []
},
这样的办法应该行得通
fighters.forEach((fighter) => {
let data = '';
for (const key in fighter.output) {
if (Array.isArray(fighter.output[key])) {
data += JSON.stringify(fighter.output[key]) + ',';
} else if (typeof fighter.output[key] === 'object') {
data += JSON.stringify(fighter.output[key]) + ',';
} else {
data += fighter.output[key] + ',';
}
}
data = data.replace(/(^,)|(,$)/g, ""); // Remove trailing comma
data += '\n'; // Add new line
fs.appendFile('data.csv', data, (err) => {
if (err) throw err;
});
});
没有测试代码。此外,根据数据大小,创建data.csv
文件可能需要一些时间。甚至在nodejs完成文件写入之前,forEach
外观也将完成。因此,程序可能会退出,但在后台编写可能会一直进行到完成。您必须维护async
执行部分。如何处理不在这个问题的范围之内
fighters.forEach((fighter) => {
let data = '';
for (const key in fighter.output) {
if (Array.isArray(fighter.output[key])) {
data += JSON.stringify(fighter.output[key]) + ',';
} else if (typeof fighter.output[key] === 'object') {
data += JSON.stringify(fighter.output[key]) + ',';
} else {
data += fighter.output[key] + ',';
}
}
data = data.replace(/(^,)|(,$)/g, ""); // Remove trailing comma
data += '\n'; // Add new line
fs.appendFile('data.csv', data, (err) => {
if (err) throw err;
});
});