我有一个程序,它在CSV文件中生成报告。问题是,时间戳是Unix时间戳格式。所以我需要在forEach循环中访问特定的时间戳元素并修改它。我是JavaScript新手。如果是Python,那就没问题了。
代码如下所示:
for (let electronicRecord of result.Values) {
fields.forEach((f, i) => csvData += electronicRecord[f] + (i == fields.length -1 ? "\n" : delimiter));
}
我知道我想要更改的值是“ElectronicRecord[“TimeStamp”],但是我如何在forEach循环中访问它呢?
for (let i in fields) {
if (i == "TimeStamp") {
csvData += electronicRecord[i]
} else if (fields.indexOf(i) == fields.length - 1) {
csvData += electronicRecord[i] + "\n";
} else {
csvData += electronicRecord[i] + delimiter;
}
}
这是我的主意,但似乎不起作用。fields是一个数组,其中包含CSV文件的“headlines”。
我的系统中的原始函数是:
// Please note, the format of a file path is device and operating system specific
// You need to adapt the given path to your environment
// Example for a Unified Comfort device (Linux OS): /home/user1/data.dat
// Example for a Unified PC-based device (MS Windows OS): C:\\Users\\Public\\data.dat
const startDate = "2020-09-01 08:00:00.000";
const endDate = "2020-10-01 08:00:00.000";
let moreFollows = false;
let page = 0;
let fields = ["TimeStamp", "AuditProviderType", "AuditProvider", "ObjectReference","ObjectName", "OperationType", "OperatorStation", "User", "OldValue", "NewValue", "Reason", "Language", "Signature", "Integrity"];
let delimiter = ",";
let csvData = "";
fields.forEach((f, i) => csvData += f + (i == fields.length - 1 ? "\n" : delimiter));
do {
try {
const result = await HMIRuntime.Audit.SysFct.ReadElectronicRecord(startDate, endDate, page);
moreFollows = result.More;
for (let electronicRecord of result.Values) {
fields.forEach((f, i) => csvData += electronicRecord[f] + (i == fields.length -1 ? "\n" : delimiter));
}
page++;
} catch(ex) {
HMIRuntime.Trace("Error reading electronic record. Error: " + ex.message);
return;
}
} while (moreFollows);
try {
let fileName = "C:\\Users\\Public\\AuditFile.csv";
await HMIRuntime.FileSystem.WriteFile(fileName, csvData, "utf8");
HMIRuntime.Trace("Write file finished successfully");
} catch(ex) {
HMIRuntime.Trace("Error writing file. Error: " + ex.message);
}
1条答案
按热度按时间xt0899hw1#
要访问forEach循环中每个“electronicRecord”对象的“TimeStamp”值,可以修改循环以使用for...of语法,然后访问“TimeStamp”属性: