What is the best way to get values from a set of objects coming through my function and write those values into a csv file?
通过我的函数从一组对象中获取值并将这些值写入csv文件的最佳方法是什么?
The objects will come through like this:
对象将通过这样的方式来实现:
[ { objectTo:
{ employeeName: 'John',
employeeID: '234234',
DOB: '2333'},
employeeCurrent: true,
employeePast: false},
{ objectTo:
{ employeeName: 'Janet',
employeeID: '23423432',
DOB: '23333' },
employeeCurrent:true,
employeePast: false} ]
I want to get the objects like objects1.objectTo.employeeName, objects1.objectTo.DOB, objects1.employeeCurrent
on one cell but write each objectTo in it's own row.
我想在一个单元格上获取objects1.objectTo.employeeName,objects1.objectTo.DOB,objects1.employeeCurrent等对象,但是将每个objectTo写入它自己的行中。
I've tried to loop through and set this using a for loop then passing in the value to writeFile but this doesn't work. What is the best way to do this?
我试图循环并使用for循环设置它然后将值传递给writeFile但这不起作用。做这个的最好方式是什么?
I've also tried to use the library underscore to give me the values but this also is not working and just writes out the whole objects:
我也尝试使用库下划线给我值,但这也不起作用,只写出整个对象:
var objectsToFile = function(objectsTotal){
objectsTotal = _.values(objectsTotal, function(value) {
return value.objectTo.employeeName;
});
objectsTotal = _.values(objectsTotal, function(value) {
return value.employeeCurrent;
});
objectsTotal = _.values(objectsTotal, function(value) {
return value.employeePast;
});
writeFile('objectsTotalet.csv', util.inspect(objectsTotal), function(err) {
if (err) console.log(err);
});
};
Expected output in CSV (objects1.objectTo.employeeName, objects1.objectTo.DOB, objects1.employeeCurrent):
CSV中的预期输出(objects1.objectTo.employeeName,objects1.objectTo.DOB,objects1.employeeCurrent):
John 2333 true Janet 23333 true
约翰福音2333真珍妮特23333真实
1 个解决方案
#1
0
let obj = [ { objectTo:
{ employeeName: 'John',
employeeID: '234234',
DOB: '2333'},
employeeCurrent: true,
employeePast: false},
{ objectTo:
{ employeeName: 'Janet',
employeeID: '23423432',
DOB: '23333' },
employeeCurrent:true,
employeePast: false} ]
const csvString = obj.map(item => {
return [item.objectTo.employeeName, item.objectTo.DOB, item.employeeCurrent]
}).join('\n')
console.log(csvString)
// John,2333,true
// Janet,23333,true
If you want both of them on the same row, just join them using ','
instead of '\n'
如果你想让它们都在同一行,只需使用','而不是'\ n'加入它们
Once you have your string, you can use fs
to write them in a csv
file
获得字符串后,可以使用fs将它们写入csv文件中
#1
0
let obj = [ { objectTo:
{ employeeName: 'John',
employeeID: '234234',
DOB: '2333'},
employeeCurrent: true,
employeePast: false},
{ objectTo:
{ employeeName: 'Janet',
employeeID: '23423432',
DOB: '23333' },
employeeCurrent:true,
employeePast: false} ]
const csvString = obj.map(item => {
return [item.objectTo.employeeName, item.objectTo.DOB, item.employeeCurrent]
}).join('\n')
console.log(csvString)
// John,2333,true
// Janet,23333,true
If you want both of them on the same row, just join them using ','
instead of '\n'
如果你想让它们都在同一行,只需使用','而不是'\ n'加入它们
Once you have your string, you can use fs
to write them in a csv
file
获得字符串后,可以使用fs将它们写入csv文件中