簡體   English   中英

如何在節點js中添加鍵和數組對象

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM