简体   繁体   English

如何在节点js中添加键和数组对象

[英]How to add key in along with array object in node js

Here is my a resultant array and now I want to add extra key along with current key. 这是我的结果数组,现在我想与当前键一起添加额外的键。 tried . 试过了。 push is not a function 推送不是功能

aa[0]['new_key'] = 'blah';    // is not working

My array is looks like. 我的数组看起来像。

 var aa =  [
        {
          "main": "56d940bb2b5916181d0906e2",
          "current": 6544
        }
      ]

And my original code is using mongoDB query. 我的原始代码是使用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);
            }
        });

I also get same problem a lot i resolved this issue by using this 我也遇到了同样的问题,我通过使用这个解决了这个问题

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.

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