简体   繁体   English

如何在现有 JSON 中添加新字段

[英]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.我有 JSON 有 10000 条唯一记录,我需要为每条记录添加另一个具有唯一值的字段。 How do I add that?我该如何添加?

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

The JSON is stored in variable showWard . JSON 存储在变量showWard中。 How do I add an action field in my JSON with value = './wardName' where wardName is already a field in my JSON?如何在我的 JSON 中添加一个值 = './wardName' 的操作字段,其中 wardName 已经是我的 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() ?使用.map() I don't know the logic for determinate the gender, but in Ward.find() callback you can add a thing like that:我不知道确定性别的逻辑,但在Ward.find()回调中你可以添加这样的东西:

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

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

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