左右加入

时间:2016-07-01 19:45:26

标签: javascript rethinkdb

我有一个查询,它使用left从两个不同的表中返回以下数据。我希望将rightzip()合并,而不是执行name(重写joined_atserver_info),我想添加右对象的属性将一个属性保留为名为zip的属性,并删除'left'并使其成为单个对象,就像在[{ "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",
    }
}]

预期结果

<characteristic uuid="xxxxx-xxxx-xxxxx-xxxx-xxxxxxxxxx" id="configuration">
    <description>Config Register</description>
    <properties read="true" write="true"/>
    <value type="hex">00</value>
</characteristic>

3 个答案:

答案 0 :(得分:1)

我对rethinkdb一无所知,但如果你没问题,可以使用普通的Javascript,Object.assign来完成。

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

如果您愿意,当然可以在结果中编写新的Object:

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

答案 1 :(得分:1)

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

答案 2 :(得分:0)

遍历列表中的所有对象,并在right键/属性下的left对象中添加server_info对象,然后删除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;
});