簡體   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