项目场景:
需要批量操作数万条json数据,包括对其进行字段的剔除与增加,最终将新数据保存至文件中。
解决方案:
const data = [
{
label: 'False',
},
{
label: 'True'
},
]
// delete
const newData = data.filter((item) => item.label !== 'False')
// console.log(newData)
// add
const newData1 = data.fill((item) => item["label"] = 'False')
const fs = require('fs');
const da = JSON.stringify(newData);
fs.writeFile('yylwc.json', da, (err) => {
if (err) {
throw err;
}
console.log("JSON data is saved.");
});