How would one convert the following:
[{
"name": "annoying data",
"status": "evil",
"frustration": [
{
"name": "foo",
"value": 1
},
{
"name": "bar",
"value": 2
},
{
"name": "baz",
"value": 3
}
]
}...]
To this output:
[{
"name": "annoying data",
"status": "evil",
"foo": 1,
"bar": 2,
"baz": 3
}...]
from within a rethink query?
We have a stored third-party API response that I am attempting to transform into something useful. In my mind there are 2 things I need to accomplish to reach my end goal of sane data output.
- transform the
frustrationarray into an object usingnameas the keys andvalueas the values. - merge the newly transformed
frustrationobject into the parent object.
I have a possible solution for step 2:
.map(function(row) {
row.pluck('name', 'status').merge(row('frustration'))
})
But I'm getting nowhere with step one. Any ideas are much appreciated!
Current full query (names changed to protect... me!):
r.db('test').table('evil_things').map(function(row) {
var x = row('a')('tree')('children')
.nth(0)('children')
.nth(0)
.map(function(x) {
return x('children');
})
.concatMap(function(x) {
return x.pluck('name', 'value');
})
;
return {
'name': row('name'),
'status': row('status'),
'frustration': x
};
}).filter(function(row) {
return r.expr(['FOO','BAR','BAZ']).contains(row('name').upcase());
})
.orderBy('status');