У меня есть следующие примеры данных,
[
{
"_id": "5e88624da92cf342ac9357a3",
"defaultAccountId": 1009,
"grnNo": "17 - 20/21",
"party": "Adash",
"buyer": "Amurtha Knit Lab",
"lotNo": "1005",
"orderNo": "5005",
"dyeingDcNo": "52003",
"dyeingDcDate": "2020-03-04T05:09:05.584Z",
"dyeingName": "sathya",
"partyDcNo": "1005",
"partyDcRolls": 454545,
"partyDcDate": "2020-03-04T05:09:05.584Z",
"weight": 25.366,
"reference": "naveen",
"operationDisplay": [
{
"operationStatus": "Completed",
"completedAt": "2020-04-04T10:32:45.705Z",
"processName": "Heating",
"lab": "amurtha",
"machine": "high quality",
"weight": 52.365,
"effect": "bad",
"setWidth": 56.56455,
"afterWidth": 56.56455,
"afterDia": 56.56455,
"afterGsm": 56.56455,
"repeat": "yes",
"temp": "high",
"speed": "nedd for speed",
"remarks": "nedd improvement"
},
{
"operationStatus": "Completed",
"completedAt": "2020-04-04T10:32:45.706Z",
"processName": "Heating",
"lab": "amurtha",
"machine": "high quality",
"weight": 52.365,
"effect": "bad",
"setWidth": 56.56455,
"afterWidth": 56.56455,
"afterDia": 56.56455,
"afterGsm": 56.56455,
"repeat": "yes",
"temp": "high",
"speed": "nedd for speed",
"remarks": "nedd improvement"
},
{
"operationStatus": "Completed",
"completedAt": "2020-04-04T10:32:45.706Z",
"processName": "Heating",
"lab": "amurtha",
"machine": "high quality",
"weight": 52.365,
"effect": "bad",
"setWidth": 56.56455,
"afterWidth": 56.56455,
"afterDia": 56.56455,
"afterGsm": 56.56455,
"repeat": "yes",
"temp": "high",
"speed": "nedd for speed",
"remarks": "nedd improvement"
},
{
"operationStatus": "Completed",
"completedAt": "2020-04-04T10:32:45.706Z",
"processName": "Heating",
"lab": "amurtha",
"machine": "high quality",
"weight": 52.365,
"effect": "bad",
"setWidth": 56.56455,
"afterWidth": 56.56455,
"afterDia": 56.56455,
"afterGsm": 56.56455,
"repeat": "yes",
"temp": "high",
"speed": "nedd for speed",
"remarks": "nedd improvement"
}
],
"orderHistory": [
{
"Date": "2020-04-04T10:32:45.708Z",
"type": "water",
"no": "5a545s",
"rolls": 5455,
"weight": 5454.554
}
],
"status": "Completed",
"createdByName": "naveen kumar",
"createdById": "4545a45a445",
"fabricArray": [],
"processArray": [],
"createdDate": "2020-04-04T10:32:45.708Z",
"id": "9f6fb3d0-765f-11ea-9ec4-594499e83360",
"AI": 76,
"__v": 0,
"no": "76 - 20/21"
},
{
"_id": "5e88629d31683d4f3ccc95e8",
"defaultAccountId": 1009,
"grnNo": "17 - 20/21",
"party": "Adash",
"buyer": "Amurtha Knit Lab",
"lotNo": "1005",
"orderNo": "5005",
"dyeingDcNo": "52003",
"dyeingDcDate": "2020-03-04T05:09:05.584Z",
"dyeingName": "sathya",
"partyDcNo": "1005",
"partyDcRolls": 454545,
"partyDcDate": "2020-03-04T05:09:05.584Z",
"weight": 25.366,
"reference": "naveen",
"operationDisplay": [
{
"operationStatus": "Completed",
"completedAt": "2020-04-04T10:34:05.094Z",
"processName": "Heating",
"lab": "amurtha",
"machine": "high quality",
"weight": 52.365,
"effect": "bad",
"setWidth": 56.56455,
"afterWidth": 56.56455,
"afterDia": 56.56455,
"afterGsm": 56.56455,
"repeat": "yes",
"temp": "high",
"speed": "nedd for speed",
"remarks": "nedd improvement"
},
{
"operationStatus": "Completed",
"completedAt": "2020-04-04T10:34:05.094Z",
"processName": "Heating",
"lab": "amurtha",
"machine": "high quality",
"weight": 52.365,
"effect": "bad",
"setWidth": 56.56455,
"afterWidth": 56.56455,
"afterDia": 56.56455,
"afterGsm": 56.56455,
"repeat": "yes",
"temp": "high",
"speed": "nedd for speed",
"remarks": "nedd improvement"
},
{
"operationStatus": "Completed",
"completedAt": "2020-04-04T10:34:05.095Z",
"processName": "Heating",
"lab": "amurtha",
"machine": "high quality",
"weight": 52.365,
"effect": "bad",
"setWidth": 56.56455,
"afterWidth": 56.56455,
"afterDia": 56.56455,
"afterGsm": 56.56455,
"repeat": "yes",
"temp": "high",
"speed": "nedd for speed",
"remarks": "nedd improvement"
},
{
"operationStatus": "Completed",
"completedAt": "2020-04-04T10:34:05.095Z",
"processName": "Heating",
"lab": "amurtha",
"machine": "high quality",
"weight": 52.365,
"effect": "bad",
"setWidth": 56.56455,
"afterWidth": 56.56455,
"afterDia": 56.56455,
"afterGsm": 56.56455,
"repeat": "yes",
"temp": "high",
"speed": "nedd for speed",
"remarks": "nedd improvement"
}
],
"orderHistory": [
{
"Date": "2020-04-04T10:34:05.096Z",
"type": "water",
"no": "5a545s",
"rolls": 5455,
"weight": 5454.554
}
],
"status": "Completed",
"createdByName": "naveen kumar",
"createdById": "4545a45a445",
"fabricArray": [],
"processArray": [],
"createdDate": "2020-04-04T10:34:05.097Z",
"id": "cec180a0-765f-11ea-95c5-27ad067619f6",
"AI": 77,
"__v": 0,
"no": "77 - 20/21"
},
{
"_id": "5e88630031683d4f3ccc95e9",
"defaultAccountId": 1009,
"grnNo": "17 - 20/21",
"party": "Adash",
"buyer": "Amurtha Knit Lab",
"lotNo": "1005",
"orderNo": "5005",
"dyeingDcNo": "52003",
"dyeingDcDate": "2020-03-04T05:09:05.584Z",
"dyeingName": "sathya",
"partyDcNo": "1005",
"partyDcRolls": 454545,
"partyDcDate": "2020-03-04T05:09:05.584Z",
"weight": 25.366,
"reference": "naveen",
"operationDisplay": [],
"orderHistory": [
{
"Date": "2020-04-04T10:35:44.256Z",
"type": "water",
"no": "5a545s",
"rolls": 5455,
"weight": 5454.554
}
],
"status": "Completed",
"createdByName": "naveen kumar",
"createdById": "4545a45a445",
"fabricArray": [],
"processArray": [],
"createdDate": "2020-04-04T10:35:44.256Z",
"id": "09dbf710-7660-11ea-95c5-27ad067619f6",
"AI": 78,
"__v": 1,
"no": "78 - 20/21"
},
{
"_id": "5e8863910eb0363ad46e9fc6",
"defaultAccountId": 1009,
"grnNo": "17 - 20/21",
"party": "Adash",
"buyer": "Amurtha Knit Lab",
"lotNo": "1005",
"orderNo": "5005",
"dyeingDcNo": "52003",
"dyeingDcDate": "2020-03-04T05:09:05.584Z",
"dyeingName": "sathya",
"partyDcNo": "1005",
"partyDcRolls": 454545,
"partyDcDate": "2020-03-04T05:09:05.584Z",
"weight": 25.366,
"reference": "naveen",
"operationDisplay": [],
"orderHistory": [
{
"Date": "2020-04-04T10:38:09.114Z",
"type": "water",
"no": "5a545s",
"rolls": 5455,
"weight": 5454.554
}
],
"status": "Completed",
"createdByName": "naveen kumar",
"createdById": "4545a45a445",
"fabricArray": [],
"processArray": [],
"createdDate": "2020-04-04T10:38:09.114Z",
"id": "60338ab0-7660-11ea-b15f-615914b43a17",
"AI": 79,
"__v": 1,
"no": "79 - 20/21"
},
{
"_id": "5e8863b90eb0363ad46e9fc7",
"defaultAccountId": 1009,
"grnNo": "17 - 20/21",
"party": "Adash",
"buyer": "Amurtha Knit Lab",
"lotNo": "1005",
"orderNo": "5005",
"dyeingDcNo": "52003",
"dyeingDcDate": "2020-03-04T05:09:05.584Z",
"dyeingName": "sathya",
"partyDcNo": "1005",
"partyDcRolls": 454545,
"partyDcDate": "2020-03-04T05:09:05.584Z",
"weight": 25.366,
"reference": "naveen",
"operationDisplay": [],
"orderHistory": [
{
"Date": "2020-04-04T10:38:49.166Z",
"type": "water",
"no": "5a545s",
"rolls": 5455,
"weight": 5454.554
}
],
"status": "Completed",
"createdByName": "naveen kumar",
"createdById": "4545a45a445",
"fabricArray": [],
"processArray": [],
"createdDate": "2020-04-04T10:38:49.167Z",
"id": "7812fdf0-7660-11ea-b15f-615914b43a17",
"AI": 80,
"__v": 1,
"no": "80 - 20/21"
}
]
Мне нужно получить доступ к каждому массиву в этих данных, в котором каждый массив имеет подполя. Мне нужно отобразить подполя в файле CSV. У меня есть большое количество данных в моей базе данных. Мне нужно экспортировать его в файл CSV. Я использовал json2csv для преобразования файлов CSV. Но я не могу получить доступ к значениям полей массива, если не упомяну индекс индекса. Мне нужно получить доступ к каждому полю и полю массива без упоминания индекса массива.
Мой код:
const { Parser, transforms: { unwind, flatten } } = require('json2csv');
const fs = require('fs');
var userss = await despatch.find({ createdDate: { "$gte": start, "$lte": end }, defaultAccountId: userParam.defaultAccountId, status: 'Completed' });
// const fields = ['operationDisplay.processName'];
const fields = [
{ value: "id", label: "id" },
{ value: "defaultAccountId", label: "DefaultAccountId" },
{ value: "color", label: "Color" },
{ value: "date", label: "Date" },
{ value: "grnNo", label: "GRN_No" },
{ value: "party", label: "Party" },
{ value: "buyer", label: "Buyer" },
{ value: "lotNo", label: "LotNo" },
{ value: "orderNo", label: "OrderNo" },
{ value: "manualDcNo", label: "ManualDcNo" },
{ value: "dyeingDcNo", label: "DyeingDcNo" },
{ value: "dyeingDcDate", label: "DyeingDcDate" },
{ value: "dyeingName", label: "dyeingName" },
{ value: "partyDcNo", label: "PartyDcNo" },
{ value: "partyDcRolls", label: "PartyDcRolls" },
{ value: "weight", label: "Weight" },
{ value: "vehicleNo", label: "VehicleNo" },
{ value: "driverName", label: "DriverName" },
{ value: "deliveryReference", label: "DeliveryReference" },
{ value: "deliverTo", label: "deliverTo" },
{ value: "reference", label: "Reference" },
{ value: "referName", label: "ReferName" },
{ value: "weight", label: "Weight" },
{ value: "fabricArray.color", label: "fabricArray_Color" },
{ value: "operationDisplay", label: "ProcessName" },
{ value: "operationDisplay.processName", label: "ProcessNames" },
]
unwind({ paths: ['operationDisplay', 'operationDisplay.processName'] });
flatten({ objects: false, arrays: true });
const json2csvParser = new Parser({ fields });
const csv = json2csvParser.parse(userss);
fs.writeFile(filePath, csv, function(err) {
if (err) throw err;
console.log('file saved');
});
Пожалуйста, кто-нибудь, помогите мне с этим, я борюсь с этим в течение длительного времени , Заранее спасибо. Я включил вывод моего файла CSV здесь. . Пожалуйста, посмотрите на него и предоставьте мне решение