[英]How to add key in along with array object in node js
这是我的结果数组,现在我想与当前键一起添加额外的键。 试过了。 推送不是功能
aa[0]['new_key'] = 'blah'; // is not working
我的数组看起来像。
var aa = [
{
"main": "56d940bb2b5916181d0906e2",
"current": 6544
}
]
我的原始代码是使用mongoDB查询。
connection.modal.find( { 'user_id' : user_id }, { 'abc.def' : true}, function ( err, result ) {
if ( !err ) {
if( result.length ) {
result[0]['abc'].def[0]['new_key'] = 'blah';
response['success'] = true;
response['result'] = result[0]['abc'].def;
response['msg'] = 'data fetch';
res.json(response);
} else {
response['success'] = false;
response['result'] = '';
response['msg'] = 'No record found';
res.json(response);
}
} else {
response['success'] = false;
response['result'] = '';
response['msg'] = 'Error';
res.json(response);
}
});
我也遇到了同样的问题,我通过使用这个解决了这个问题
connection.modal.find( { 'user_id' : user_id }, { 'abc.def' : true}, function ( err, result ) {
/// ADD THIS LINE /////////
result = JSON.parse(JSON.stringify(result));
....
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.