javascript - 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);
});
});
解决方案
使用.map()
? 我不知道确定性别的逻辑,但在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);
});
});
推荐阅读
- jquery - Bootstrap v3.3.7:不能做下拉导航栏
- ionic-framework - Ionic - setRoot 和 push 之间的区别
- python - Python Pandas 保留特定值的第一次出现并删除具有相同特定值的其余行
- react-native - React Native [Section List ] 在部分上动态添加项目
- python - 将数据框系列中的分数转换为浮点数
- python - 如何在列表中递增并用它们的计数替换某些值
- r - 如何检查日期是否在 R 中的两个值之间?
- python - 调整 n 维图像数组的大小
- c# - 使用 servicestack 自动查询时的 DateTime 属性问题
- android - 我关于 Android Studio 3.3.2 库修订版 28.0.0 的问题