加入左和右

Joining left and right

我有一个查询 return 使用 eqjoin 来自两个不同表的以下数据。我想合并 leftright,但我不想做 zip()(重写 namejoined_at),而是想添加将右侧对象的属性左移到名为 server_info 的 属性 中,同时删除 'left' 并使其成为单个对象,就像在 zip 操作之后一样,我如何直接执行此操作在查询中。

当前结果

[{
    "left":{
       "avatar":"a29f54048d9ec6c00913057333160a3e",
       "joined_at":"Thu Feb 25 2016 21:29:07 GMT+00:00",
       "name":"Zephy",
       "uid":"132166948359241728"
    },
    "right":{
       "icon":"2aab26934e72b4ec300c5aa6cf67c7b3",
       "id":"81384788765712384",
       "member_count":7888,
       "name":"Discord API",
       "owner_id":"53905483156684800",
    }
}]

预期结果

[{
    "avatar":"a29f54048d9ec6c00913057333160a3e",
    "joined_at":"Thu Feb 25 2016 21:29:07 GMT+00:00",
    "name":"Zephy",
    "uid":"132166948359241728"
    "server_info": {
       "icon":"2aab26934e72b4ec300c5aa6cf67c7b3",
       "id":"81384788765712384",
       "member_count":7888,
       "name":"Discord API",
       "owner_id":"53905483156684800",
    }
}]

遍历列表中的所有对象并在 server_info key/property 下的 left 对象内添加 right 对象,然后删除 right 对象。

var arr = [{
"left":{
   "avatar":"a29f54048d9ec6c00913057333160a3e",
   "joined_at":"Thu Feb 25 2016 21:29:07 GMT+00:00",
   "name":"Zephy",
   "uid":"132166948359241728"
},
"right":{
   "icon":"2aab26934e72b4ec300c5aa6cf67c7b3",
   "id":"81384788765712384",
   "member_count":7888,
   "name":"Discord API",
   "owner_id":"53905483156684800",
}
}];

arr.forEach(function(obj){
   obj.left.server_info = obj.right;
   delete obj.right;
});

我对 rethinkdb 一无所知,但如果你觉得可以的话,你可以用普通的 Javascript,用 Object.assign.

var newObject = Object.assign(result[0].left, {server_info: result[0].right)};

如果你愿意,当然可以在结果本身中写入新对象:

result = Object.assign(result[0].left, {server_info: result[0].right)};

您可以在查询末尾添加 .map(function(row) { return row('left').merge({'server_info': row('right')}); }) 以获得该效果。