简体   繁体   English

Rethinkdb将对象数组转换为单个对象

[英]Rethinkdb convert an array of objects to a single object

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. 我们有一个存储的第三方API响应,我试图转换为有用的东西。 In my mind there are 2 things I need to accomplish to reach my end goal of sane data output. 在我看来,我需要完成两件事才能实现理智的数据输出。

  1. transform the frustration array into an object using name as the keys and value as the values. 使用name作为键并将value作为值,将frustration数组转换为对象。
  2. merge the newly transformed frustration object into the parent object. 将新转换的frustration对象合并到父对象中。

I have a possible solution for step 2: 我有第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');

You are in correct way. 你是正确的。

You can create an objects from a key-value list of data with object . 您可以使用对象从键值数据列表创建object But object receive a list of parameters, instead of an array so you need args . 但是object接收参数列表而不是数组,因此您需要args

frustation is an array with each document as object, you need a way to turn it into a list of flatten array, meaning from this array frustation是一个数组,每个文档都作为对象,你需要一种方法将它变成一个flatten数组的列表,意思是从这个数组

[{key1: val1}, {key2: val2},...]

we turn it to 我们把它转向

[key1, val1, key2, val2,...]

to feed into args for object . object提供args That's the job of concatMap . 这是concatMap的工作。

Let's try this: 我们试试这个:

r.expr([{
  "name": "annoying data",
  "status": "evil",
  "frustration": [
    {
      "name": "foo",
      "value": 1
    },
    {
      "name": "bar",
      "value": 2
    },
    {
      "name": "baz",
      "value": 3
    }
  ]
}])
.map(function(doc) {
  return doc.pluck("name", "status").merge(
    r.object(r.args(doc('frustration')
     .concatMap(function(frustration) { return [frustration("name"), frustration("value")] })
    ))
  )
})

Result: 结果:

[
{
"bar": 2 ,
"baz": 3 ,
"foo": 1 ,
"name":  "annoying data" ,
"status":  "evil"
}
]

Hope it helps: 希望能帮助到你:

Something like this should work: 这样的事情应该有效:

.map(function(row) {
  return row.pluck('name', 'status').merge(
    row('frustration').map(function(x){ return [x['name'], x['value']]; }).coerceTo('object'))
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM