我使用mongodb aggregation
示例数据:
{
"name": "John wire",
"city": "New York"
},
{
"name": "mike jansen",
"city": "Dubai"
}
...etc
我的返回聚合代码是:
], function (err, result) {
if (err) {
logger.error(req.method + ": " + req.originalUrl + ", message: " + err.message)
next(createError.InternalServerError())
}
res.send(result); //this line
});
我有一个函数可以将字段返回name
到capitalize
每个第一个单词:
function titleCase(str) {
var splitStr = str.toLowerCase().split(' ');
for (var i = 0; i < splitStr.length; i++) {
// You do not need to check if i is larger than splitStr length, as your for does that for you
// Assign it back to the array
splitStr[i] = splitStr[i].charAt(0).toUpperCase() + splitStr[i].substring(1);
}
// Directly return the joined string
return splitStr.join(' ');
}
示例:约翰线 -> 约翰线
如何在result.name
数据返回之前格式化数据?
谢谢你。
您可以使用 ES6 引入的扩展运算符:
result.map(o => ({ ...o, name: titleCase(o.name)}));
ES5:
result.map(o => Object.assign(o, {name: titleCase(o.name)}));
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句