Reputation: 144
I have a query that return the following data from two different tables using eqjoin
. What I would like to combine left
and right
, but instead of doing a zip()
(that rewrites the name
and joined_at
), I want to add the right object's properties to left one into a property named server_info
and also delete the 'left' and make it a single object like after a zip
operation, how can I do this directly in the query.
Current result
[{
"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",
}
}]
Expected result
[{
"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",
}
}]
Upvotes: 0
Views: 85
Reputation: 5289
You can add .map(function(row) { return row('left').merge({'server_info': row('right')}); })
to the end of your query to get that effect.
Upvotes: 1
Reputation: 7360
I know nothing about rethinkdb, but if it is ok for you you can do it with plain Javascript, with Object.assign.
var newObject = Object.assign(result[0].left, {server_info: result[0].right)};
If you want you can, of course, write the new Object in result itself:
result = Object.assign(result[0].left, {server_info: result[0].right)};
Upvotes: 1
Reputation: 22885
Iterate over all objects in list and add right
object inside left
object under server_info
key/property and then delete right
object.
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;
});
Upvotes: 0