puning-real-estate/remove-empty-license.js

27 lines
1.0 KiB
JavaScript

import fs from 'fs/promises';
async function removeEmptyLicenseRecords() {
try {
const filePath = './data.json';
const data = await fs.readFile(filePath, 'utf-8');
let records = JSON.parse(data);
// Filter out records where "许可证号" is "空"
const filteredRecords = records.filter(record => record['许可证号'] !== '空');
// Update the sequential "序号" field for the remaining records
for (let i = 0; i < filteredRecords.length; i++) {
filteredRecords[i]['序号'] = (i + 1).toString();
}
await fs.writeFile(filePath, JSON.stringify(filteredRecords, null, 4), 'utf-8');
console.log(`已从 data.json 中删除所有 "许可证号" 为 "空" 的数据。共删除 ${records.length - filteredRecords.length} 条记录。`);
console.log('序号字段已重新编号以保持顺序。');
} catch (error) {
console.error('删除空许可证号数据时发生错误:', error.message);
}
}
removeEmptyLicenseRecords();