一個如何將轉換如下:從重新考慮查詢內Rethinkdb轉換對象的數組到單個對象
[{
"name": "annoying data",
"status": "evil",
"foo": 1,
"bar": 2,
"baz": 3
}...]
:
[{
"name": "annoying data",
"status": "evil",
"frustration": [
{
"name": "foo",
"value": 1
},
{
"name": "bar",
"value": 2
},
{
"name": "baz",
"value": 3
}
]
}...]
向該輸出?
我們有一個存儲的第三方API響應,我試圖轉換成有用的東西。在我看來,爲了達到理智的數據輸出的最終目標,我需要完成兩件事情。
- 變換
frustration
陣列分爲使用name
作爲鍵和value
作爲值的對象。 - 將新轉換的
frustration
對象合併到父對象中。
我有步驟2中可能的解決方案:
.map(function(row) {
row.pluck('name', 'status').merge(row('frustration'))
})
但我與步驟一一事無成。任何想法都非常感謝!
當前完整的查詢(名稱變更爲保護......我!):
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');
真棒謝謝你推動正確的方向。 – scull7
嘿Scull7,你可以閱讀更多關於這本書(我的書btw):https://github.com/kureikain/simplyrethink/ – kureikain