繁体   English   中英

如何在 JSON 对象中添加多个值并获取更新的 json 文件

[英]How to add multiple values in JSON object and get an updated json file

我有以下 JSON 文件

var info = [{
     "name" : "john",
     "address" : "32 street, london",
     "contact" : 123456
},{
     "name" : "jeyy",
     "address" : "51 street, new york",
     "contact" : 987654321,
     "gender" : "male"
},{
     "name" : "robert",
     "address" : "14th street, birmingham",
     "contact" : 456123789,
     "gender" : "male"
},{
     "name" : "carlos",
     "address" : "89th street, manchester",
     "contact" : 23456
},{
     "name": "johnny",
     "address": "6th street, Washington",
     "contact": 23456
},{
     "name": "jack",
     "address": "4th street, VA",
     "contact": 23456,
     "gender": "male"
}
];

如您所见,我在某些数组中缺少 "gender" = "male" 对象。 如何将它们添加到丢失的对象中而不是将它们添加到已有的对象中。 另外我将如何获得新的更新文件。

在这里,我使用forEach()遍历数组并检查每个对象是否在gender属性中具有值。 如果没有,则给它一个值“男性”。

 var info = [{ "name" : "john", "address" : "32 street, london", "contact" : 123456 },{ "name" : "jeyy", "address" : "51 street, new york", "contact" : 987654321, "gender" : "male" },{ "name" : "robert", "address" : "14th street, birmingham", "contact" : 456123789, "gender" : "male" },{ "name" : "carlos", "address" : "89th street, manchester", "contact" : 23456 },{ "name": "johnny", "address": "6th street, Washington", "contact": 23456 },{ "name": "jack", "address": "4th street, VA", "contact": 23456, "gender": "male" } ]; info.forEach(i => { if(!i.gender) i.gender = "male"; }); console.log(info);

这将解决-

 var info = [{ "name" : "john", "address" : "32 street, london", "contact" : 123456 },{ "name" : "jeyy", "address" : "51 street, new york", "contact" : 987654321, "gender" : "male" },{ "name" : "robert", "address" : "14th street, birmingham", "contact" : 456123789, "gender" : "male" },{ "name" : "carlos", "address" : "89th street, manchester", "contact" : 23456 },{ "name": "johnny", "address": "6th street, Washington", "contact": 23456 },{ "name": "jack", "address": "4th street, VA", "contact": 23456, "gender": "male" } ]; info.forEach((e)=>{ var t=Object.keys(e); if(t.indexOf('gender')==-1) e.gender='male'; }) console.log(info);

您可以在该对象上使用.hasOwnProperty来找出哪个对象没有属性gender然后反复添加它并再次写回文件!


这是帮助您入门的代码片段。

const fs = require('fs');
const util = require('util');

// Promisify fs api(s)
const readFileAsync = util.promisify(fs.readFile);
const writeFileAsync = util.promisify(fs.writeFile);

// IIFE to read the file, find the objects which has missing properties, add them and write them back
(async function() {
    try {
        const fileContents = await readFileAsync('data.json');
        const fileData = JSON.parse(fileContents);

        const remainingData = fileData.filter(x => !x.hasOwnProperty('gender'));

        remainingData.forEach(x => x.gender = 'Male');

        await writeFileAsync('data.json', JSON.stringify(fileData, null, 4));

    } catch(err) {
        console.log(`Failed because: {err}!`)
    }
})();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM