convert-units-to-numbers

This commit is contained in:
秦秋旭 2026-01-18 18:34:24 +08:00
parent c8d9ab1f5e
commit 7e95f56cf2
3 changed files with 502 additions and 478 deletions

View File

@ -0,0 +1,24 @@
import fs from 'fs/promises';
async function convertUnitsToNumbers() {
try {
const filePath = './data.json';
const data = await fs.readFile(filePath, 'utf-8');
let records = JSON.parse(data);
// Iterate through each record and convert the specified fields to numbers
for (const record of records) {
// Use parseInt and provide a fallback of 0 if parsing fails (e.g., for empty strings)
record['总套数'] = parseInt(record['总套数'], 10) || 0;
record['可售套数'] = parseInt(record['可售套数'], 10) || 0;
}
await fs.writeFile(filePath, JSON.stringify(records, null, 4), 'utf-8');
console.log('已将 data.json 中的 "总套数" 和 "可售套数" 字段的值转换为数字。');
} catch (error) {
console.error('转换字段为数字时发生错误:', error.message);
}
}
convertUnitsToNumbers();

956
data.json

File diff suppressed because it is too large Load Diff

BIN
data.xlsx

Binary file not shown.