我在数据下载csv中遇到了一些问题,我想将[object Object]更改为文件csv中的字符串。但是我有尝试代码的结果是不确定的。
这是我的JSON格式,我仅使用一个示例数据
[
{
"id": 117,
"code": "RR123",
"dueDate": "2018-06-25T09:51:00",
"isActive": true,
"inspectors": {
"id": 67,
"employeeNumber": "18001",
"name": "Larks Anderson",
"isActive": true
},
"questioners": {
"id": 63,
"code": "PI190",
"name": "Inspeksi Door",
"isActive": true,
"questionerDetails": [
{
"id": 124,
"questionDetail": "",
"isActive": true
}
]
}
},
]
这是我在component.ts中的代码
//Button CSV
getcsvFile() {
this.workorderService.getAllWorkOrder().subscribe(data => {
this.allpagingData = [];
let questionerlabel: any;
for (let index in data) {
console.log(data[index].questioners);
//i use this to change the [object Object], but the result is undefined in csv
for (let ai in data[index].questioners) {
questionerlabel = data[index].questioners[ai].name;
console.log(questionerlabel);
}
this.allpagingData.push({
"code": data[index].code,
"inspectors": data[index].inspectors,
"questioners": questionerlabel,
"dueDate": data[index].dueDate,
"isActive": data[index].isActive
});
}
var option = {
fieldSeparator: ',',
quoteStrings: '"',
decimalseparator: '.',
showLabels: true,
showTitle: true,
headers: ['WO Code' , 'Inspectors', 'Questioner', 'Date', 'Status']
}
new Angular2Csv(this.allpagingData, 'WorkOrder Report', option)
});
}
那么如何将[object Object]更改为字符串?
答案 0 :(得分:1)
使用JSON.stringify(object)
,它将把您的对象变成人类可读的JSON。不过,不是很确定那就是您想要的。
答案 1 :(得分:1)
您需要将“检查器”转换为字符串。所以像这样使用JSON.stringify(object)
this.allpagingData.push({
"code": data[index].code,
"inspectors": data[index].inspectors ? JSON.stringify(data[index].inspectors) : '',
"questioners": questionerlabel ? questionerlabel : 'N/A',
"dueDate": data[index].dueDate,
"isActive": data[index].isActive
});
仅提供检查员姓名:
this.allpagingData.push({
"code": data[index].code,
"inspectors": data[index].inspectors ? data[index].inspectors.name : '',
"questioners": questionerlabel ? questionerlabel : 'N/A',
"dueDate": data[index].dueDate,
"isActive": data[index].isActive
});
答案 2 :(得分:0)
以下是解决未定义和[object object]错误的最佳答案
yourdata.forEach((x, len) => {
if (x.length === undefined) {
x.values = JSON.stringify(x.values);
}
else {
let nfvdata = "";
let nfvobject = {};
x.forEach(y => {
if (typeof (y) === "string") {
nfvobject["questioners"] = y;
}
else if (y["questioners"] == undefined) {
nfvdata = nfvdata + JSON.stringify(y)
nfvobject["questionerDetails"] = nfvdata;
}
else {
//yourdata.push(y);
}
});
yourdata.splice(len, 1, nfvobject);
// yourdata.push(nfvobject);
}
});
let record = yourdata[1] == undefined ? dataSet["data"] : dataSet["data"][1];
this.csvOptions.headers = Object.keys(record);
new AngularCsv(yourdata, this.Filename, this.csvOptions);
这是另一种像您一样处理JSON的方法
yourdata.forEach((x, len) => {
if (x.length === undefined) {
x.values = JSON.stringify(x.values);
}
else {
yourdata.splice(len, 1);
x.forEach(y => {
yourdata.push(y);
})
}
});
let record = yourdata[1] == undefined ? yourdata : yourdata[1];
this.csvOptions.headers = Object.keys(record);
new AngularCsv(dataSet["data"], this.Filename, this.csvOptions);
}