使用使用 fast-csv 包的 node.js,我目前有这个解析功能,它读取 csv 文件、更改标题、遍历每一行并根据行数据触发事件。
validateRows: (filePath, payload, validators) => new Promise((resolve, reject) => {
const invalidRecords = [];
const validRecords = [];
fs.createReadStream(filePath)
.pipe(csv.parse({
headers: (headers) => mapHeaderToRelated(headers, payload), delimiter: ";", discardUnmappedColumns: true
}))
.validate((data, cb) => {
const errors = validators.reduce((err, func) => [...err, ...func(data)], []);
if (errors.length > 0) {
return cb(null, false, errors);
}
return cb(null, true);
})
.on("error", (error) => {
console.log("There is some error");
reject(error);
})
.on("data", (row) => {
validRecords.push(row);
})
.on("data-invalid", (row, rowNumber, reason) => {
invalidRecords.push({
data: row,
rowNumber: rowNumber,
reason: reason
});
})
.on("end", (rowCount) => {
console.log(`Parsed ${rowCount} rows. Valid Count: ${validRecords.length} Invalid Count: ${invalidRecords.length}`);
resolve({
invalidRecords,
validRecords
});
});
}),
我需要检测多次检查数字的记录。如果存在重复,例如多行具有相同的电话号码,则应将其视为无效并推送到无效记录数组
Example CSV:
| name | surname | gender | phone |
| ------ | ------- | -------- | ----- |
| John | Doe | Male | 123456 |
| Joh | Deo | Unknown | 123456 |
| Jane | Doe | Female | 999999 |
我想从解析的 CSV 中得到输出:
{
validRecords: [
{
name: Jane
surname: Doe
gender: Female
phone: 99999
}
]
invalidRecords: [
{
data: {
name: John
surname: Doe
gender: Male
phone: 123456
}
rowNumber: 1,
reason: ["Duplicate data"]
},
{
data: {
name: Joh
surname: Deo
gender: Male
phone: 123456
}
rowNumber: 2,
reason: ["Duplicate data"]
}
]
]
我该如何解决这个问题?