简体   繁体   中英

How do I add new field in existing JSON

I have JSON with 10000 unique records and I need to add another field with a unique value to every record. How do I add that?

[
{
_id: "5fffd08e62575323d40fca6f",
wardName: "CIC",
region: "ABC",
location: "AAA",
specialism: "CSR",
__v: 0
},
.
.
.
]

The JSON is stored in variable showWard . How do I add an action field in my JSON with value = './wardName' where wardName is already a field in my JSON? This is my current code:

app.get('/wards', function (req, res) {
    Ward.find({}, function (err, showWard) {
        if (err) { console.log(err); return; }
        return res.send(showWard);
    });
});

Using a .map() ? I don't know the logic for determinate the gender, but in Ward.find() callback you can add a thing like that:

app.get('/wards', function (req, res) {
    Ward.find({}, function (err, showWard) {
        if (err) { console.log(err); return; }

        const newShowWard = showWard.map(ward => {
            ward.gender = "BOH"; 
            return ward;
        })
        
        return res.send(newShowWard);
    });
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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