【问题标题】:How to add multiple values in JSON object and get an updated json file如何在 JSON 对象中添加多个值并获取更新的 json 文件
【发布时间】:2019-06-06 06:39:44
【问题描述】:

我有以下 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" 对象。如何将它们添加到缺少的对象中,而不是将它们添加到已经拥有的对象中。 另外我将如何获得新的更新文件。

【问题讨论】:

    标签: javascript node.js json


    【解决方案1】:

    这将解决-

    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);

    【讨论】:

      【解决方案2】:

      这里我使用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);

      【讨论】:

        【解决方案3】:

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


        这里有一个代码 sn-p 可以帮助您入门。

        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}!`)
            }
        })();
        

        【讨论】:

          猜你喜欢
          • 2019-07-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-03-09
          • 2022-11-27
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多