我正在使用Angular2Csv将JSON结果数据导出到CSV。但是它正在导出所有字段。是否可以仅导出特定字段而无需过滤数据?
exportToCSV = function (dataobj) {
const options = {
fieldSeparator: ',',
quoteStrings: '"',
decimalseparator: '.',
showLabels: true,
showTitle: true,
headers: Object.keys(dataobj.data[0]),
// headers: ['API Id', 'RRN', 'Test Case Group', 'Test Case Id', 'Description', 'Institution id']
};
new Angular2Csv(dataobj.data, 'My Report', options);
};
答案 0 :(得分:1)
您可以使用filter过滤出标题和数据;
const exportToCSV = function (dataobj) {debugger;
const ignoreHeaders = ["test1"];
const options = {
fieldSeparator: ',',
quoteStrings: '"',
decimalseparator: '.',
showLabels: true,
showTitle: true,
headers: Object.keys(dataobj[0]).filter(a=> ignoreHeaders.indexOf(a)===-1),
// headers: ['API Id', 'RRN', 'Test Case Group', 'Test Case Id', 'Description', 'Institution id']
};
var data= dataobj.map(a=>{
return options.headers.map(b=>{
return a[b];
})
})
console.log(options.headers)
console.log(data);
};
exportToCSV([{"test1":"1","test2":"2","test3":"3"},{"test1":"1","test2":"2","test3":"3"},{"test1":"1","test2":"2","test3":"3"}]);
答案 1 :(得分:0)
您可以创建一个新的对象资源,该资源仅包含要下载的dataobj
中的属性=>'API Id','RRN','Test Case Group','Test Case ID','Description' ,“机构ID”,然后将值从原始dataobj
对象推送到新对象,如下所示:
this.myData= new Array<dataObjDownload>();
for (let i = 0; i < this.data.length; i++) {
this.myData.push
({aPIId: this.data[i].aPIId,
rRN: this.data[i].rRN,
testCaseGroup: this.data[i].testCaseGroup,
testCaseId: this.data[i].testCaseId,
description: this.data[i].description,
Institutionid: this.data[i].Institutionid,
});
}