如何将项目添加到JSON对象

时间:2018-09-25 11:42:09

标签: javascript arrays json node.js

我有以下 post 方法:

app.post('/sound', function (req, res) {

    var myYeArray = req.body;

    let retData = [];

    for (var i = 0; i < myYeArray['filter'].length; i++) {
        if (i > 3)
            break;

        var myEl = myYeArray['filter'][i];

        myPersSaveRevEntity.myPersSaveRevEntity(myEl, function (result) {
            console.log('GUID >>> ' + i + ' : ' + myEl['_myEntityGUID'] + ' >>> ' + JSON.stringify(result));

            Object.keys(result).forEach(function (key) {
                var row = result[key];
                // add items to retData
            });

        });
    }

    res.send(retData);
});

它得到一个具有数组JSON对象的 request ;数据库行列表。

{ filter:
   [ { _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 100,
     _myEntityGUID: 101,
     _myEntityOwnerGUID: 119,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_comment',
     _myEntityType: 'my_object',
     _timeCreated: 'Sat Aug 11 05:40:39 2018\n',
     _timeUpdated: 'Sat Aug 11 05:40:39 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 1,
     _myEntityGUID: 102,
     _myEntityOwnerGUID: 119,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_kiwi',
     _myEntityType: 'my_object',
     _timeCreated: 'Sat Aug 11 06:57:04 2018\n',
     _timeUpdated: 'Sat Aug 11 06:57:04 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 1,
     _myEntityGUID: 103,
     _myEntityOwnerGUID: 119,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_video',
     _myEntityType: 'my_object',
     _timeCreated: 'Sat Aug 11 07:38:44 2018\n',
     _timeUpdated: 'Sat Aug 11 07:38:44 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 1,
     _myEntityGUID: 104,
     _myEntityOwnerGUID: 119,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_video',
     _myEntityType: 'my_object',
     _timeCreated: 'Sat Aug 11 09:11:00 2018\n',
     _timeUpdated: 'Sat Aug 11 09:11:00 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 100,
     _myEntityGUID: 105,
     _myEntityOwnerGUID: 119,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_comment',
     _myEntityType: 'my_object',
     _timeCreated: 'Sat Aug 11 09:27:22 2018\n',
     _timeUpdated: 'Sat Aug 11 09:27:22 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 1,
     _myEntityGUID: 106,
     _myEntityOwnerGUID: 119,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_memo',
     _myEntityType: 'my_object',
     _timeCreated: 'Sat Aug 11 09:56:00 2018\n',
     _timeUpdated: 'Sat Aug 11 09:56:00 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 104,
     _myEntityGUID: 107,
     _myEntityOwnerGUID: 119,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_comment',
     _myEntityType: 'my_object',
     _timeCreated: 'Sat Aug 11 10:01:28 2018\n',
     _timeUpdated: 'Sat Aug 11 10:01:28 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 1,
     _myEntityGUID: 108,
     _myEntityOwnerGUID: 119,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_profile_pics_album',
     _myEntityType: 'my_object',
     _timeCreated: 'Sat Aug 11 14:48:44 2018\n',
     _timeUpdated: 'Sat Aug 11 14:48:44 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 1,
     _myEntityGUID: 109,
     _myEntityOwnerGUID: 119,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_memo',
     _myEntityType: 'my_object',
     _timeCreated: 'Sat Aug 11 15:40:17 2018\n',
     _timeUpdated: 'Sat Aug 11 15:40:17 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 104,
     _myEntityGUID: 110,
     _myEntityOwnerGUID: 119,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_comment',
     _myEntityType: 'my_object',
     _timeCreated: 'Sat Aug 11 16:06:55 2018\n',
     _timeUpdated: 'Sat Aug 11 16:06:55 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 2,
     _myEntityGUID: 111,
     _myEntityOwnerGUID: 119,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_video',
     _myEntityType: 'my_object',
     _timeCreated: 'Sat Aug 11 22:19:29 2018\n',
     _timeUpdated: 'Sat Aug 11 22:19:29 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 1,
     _myEntityGUID: 112,
     _myEntityOwnerGUID: 119,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_appointment',
     _myEntityType: 'my_object',
     _timeCreated: 'Sun Aug 12 06:56:21 2018\n',
     _timeUpdated: 'Sun Aug 12 06:56:21 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 1,
     _myEntityGUID: 113,
     _myEntityOwnerGUID: 119,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_announcement',
     _myEntityType: 'my_object',
     _timeCreated: 'Sun Aug 12 06:57:15 2018\n',
     _timeUpdated: 'Sun Aug 12 06:57:15 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 1,
     _myEntityGUID: 114,
     _myEntityOwnerGUID: 119,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_calendar',
     _myEntityType: 'my_object',
     _timeCreated: 'Sun Aug 12 06:57:47 2018\n',
     _timeUpdated: 'Sun Aug 12 06:57:47 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 1,
     _myEntityGUID: 115,
     _myEntityOwnerGUID: 119,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_pics_album',
     _myEntityType: 'my_object',
     _timeCreated: 'Sun Aug 12 07:20:23 2018\n',
     _timeUpdated: 'Sun Aug 12 07:20:23 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 1,
     _myEntityGUID: 116,
     _myEntityOwnerGUID: 119,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_memo',
     _myEntityType: 'my_object',
     _timeCreated: 'Mon Aug 13 12:22:03 2018\n',
     _timeUpdated: 'Mon Aug 13 12:22:03 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 1,
     _myEntityGUID: 117,
     _myEntityOwnerGUID: 119,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_memo',
     _myEntityType: 'my_object',
     _timeCreated: 'Mon Aug 13 12:26:03 2018\n',
     _timeUpdated: 'Mon Aug 13 12:26:03 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 1,
     _myEntityGUID: 118,
     _myEntityOwnerGUID: 119,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_memo',
     _myEntityType: 'my_object',
     _timeCreated: 'Mon Aug 13 12:29:45 2018\n',
     _timeUpdated: 'Mon Aug 13 12:29:45 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: -1,
     _myEntityGUID: 119,
     _myEntityOwnerGUID: -1,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_user_entity',
     _myEntityType: 'my_user_entity',
     _timeCreated: 'Thu Aug 16 08:56:02 2018\n',
     _timeUpdated: 'Thu Aug 16 08:56:02 2018\n',
myUserEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: -1,
     _myEntityGUID: 120,
     _myEntityOwnerGUID: -1,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_user_entity',
     _myEntityType: 'my_user_entity',
     _timeCreated: 'Thu Aug 16 11:44:24 2018\n',
     _timeUpdated: 'Thu Aug 16 11:44:24 2018\n',
myUserEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: -1,
     _myEntityGUID: 121,
     _myEntityOwnerGUID: -1,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_user_entity',
     _myEntityType: 'my_user_entity',
     _timeCreated: 'Thu Aug 16 11:45:18 2018\n',
     _timeUpdated: 'Thu Aug 16 11:45:18 2018\n',
myUserEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: -1,
     _myEntityGUID: 122,
     _myEntityOwnerGUID: -1,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_user_entity',
     _myEntityType: 'my_user_entity',
     _timeCreated: 'Thu Aug 16 15:40:05 2018\n',
     _timeUpdated: 'Thu Aug 16 15:40:05 2018\n',
myUserEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: -1,
     _myEntityGUID: 123,
     _myEntityOwnerGUID: -1,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_user_entity',
     _myEntityType: 'my_user_entity',
     _timeCreated: 'Fri Aug 17 02:20:25 2018\n',
     _timeUpdated: 'Fri Aug 17 02:20:25 2018\n',
myUserEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 119,
     _myEntityGUID: 124,
     _myEntityOwnerGUID: 119,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_entity_info',
     _myEntityType: 'my_object',
     _timeCreated: 'Sat Aug 18 06:27:10 2018\n',
     _timeUpdated: 'Sat Aug 18 06:27:10 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 120,
     _myEntityGUID: 125,
     _myEntityOwnerGUID: 120,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_entity_social_info',
     _myEntityType: 'my_object',
     _timeCreated: 'Sun Aug 19 02:53:43 2018\n',
     _timeUpdated: 'Sun Aug 19 02:53:43 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 120,
     _myEntityGUID: 126,
     _myEntityOwnerGUID: 120,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_entity_info',
     _myEntityType: 'my_object',
     _timeCreated: 'Sun Aug 19 02:54:36 2018\n',
     _timeUpdated: 'Sun Aug 19 02:54:36 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 1,
     _myEntityGUID: 127,
     _myEntityOwnerGUID: 120,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_kiwi',
     _myEntityType: 'my_object',
     _timeCreated: 'Sun Aug 19 06:10:02 2018\n',
     _timeUpdated: 'Sun Aug 19 06:10:02 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 120,
     _myEntityGUID: 128,
     _myEntityOwnerGUID: 120,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_timeline',
     _myEntityType: 'my_object',
     _timeCreated: 'Sun Aug 19 06:10:02 2018\n',
     _timeUpdated: 'Sun Aug 19 06:10:02 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 1,
     _myEntityGUID: 129,
     _myEntityOwnerGUID: 120,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_kiwi',
     _myEntityType: 'my_object',
     _timeCreated: 'Sun Aug 19 10:41:55 2018\n',
     _timeUpdated: 'Sun Aug 19 10:41:55 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 1,
     _myEntityGUID: 130,
     _myEntityOwnerGUID: 120,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_timeline',
     _myEntityType: 'my_object',
     _timeCreated: 'Sun Aug 19 10:41:55 2018\n',
     _timeUpdated: 'Sun Aug 19 10:41:55 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 1,
     _myEntityGUID: 131,
     _myEntityOwnerGUID: 120,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_kiwi',
     _myEntityType: 'my_object',
     _timeCreated: 'Sun Aug 19 10:43:16 2018\n',
     _timeUpdated: 'Sun Aug 19 10:43:16 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 1,
     _myEntityGUID: 132,
     _myEntityOwnerGUID: 120,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_pics_album',
     _myEntityType: 'my_object',
     _timeCreated: 'Sun Aug 19 10:43:16 2018\n',
     _timeUpdated: 'Sun Aug 19 10:43:16 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 132,
     _myEntityGUID: 133,
     _myEntityOwnerGUID: 120,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_comment',
     _myEntityType: 'my_object',
     _timeCreated: 'Sun Aug 19 10:44:42 2018\n',
     _timeUpdated: 'Sun Aug 19 10:44:42 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 1,
     _myEntityGUID: 134,
     _myEntityOwnerGUID: 120,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_kiwi',
     _myEntityType: 'my_object',
     _timeCreated: 'Sun Aug 19 16:55:10 2018\n',
     _timeUpdated: 'Sun Aug 19 16:55:10 2018\n',
myObjectEntity: [Object] },
{ _id: 0,
     _myEntityAccessPermission: 0,
     _myEntityContainerGUID: 1,
     _myEntityGUID: 135,
     _myEntityOwnerGUID: 120,
     _myEntitySiteGUID: -1,
     _myEntitySubType: 'my_kiwi',
     _myEntityType: 'my_object',
     _timeCreated: 'Sun Aug 19 17:00:44 2018\n',
     _timeUpdated: 'Sun Aug 19 17:00:44 2018\n',
myObjectEntity: [Object] } ] }

我遍历JSON数组,并将每个组成JSON对象成功保存在远程数据库中。

我要失败的地方是我想创建一个JSON数组,并在持久性之后添加ach项,并将结果数组发送到 response 中。

方法myPersSaveRevEntity.myPersSaveRevEntity(myEl, function (result)返回最后一个持久表行:

GUID >>> 4 : 104 >>> [{"REMOTE_ENTITY_GUID":854,"LOCAL_ENTITY_GUID":101,"ENTITY_OWNER_GUID":119,"ENTITY_CONTAINER_GUID":100,"ENTITY_SITE_GUID":-1,"ENTITY_ACCESS_PERMISSION":0,"ENTITY_TYPE":"my_object","ENTITY_SUB_TYPE":"my_comment","COLUMN_NAME_CREATED_DATE":"Sat Aug 11 05:40:39 2018\n","COLUMN_NAME_UPDATED_DATE":"Sat Aug 11 05:40:39 2018\n"}]

我该如何完成?

谢谢大家。

0 个答案:

没有答案