2

I got some result like this in rethinkdb using group

[  
   {  
      "group":[  
         0,
         "A"
      ],
      "reduction":14
   },
   {  
      "group":[  
         "B",
         0
      ],
      "reduction":2
   }
]

I used like r.....group(..).count() (I cannot mentioned all query because there are many steps). But my result should like be displayed like this:

[  
       {  
          "group":[  
             "A"
          ],
          "reduction":14
       },
       {  
          "group":[  
             "B"
          ],
          "reduction":2
       }
    ]

Even Better just key value pair [{"A":14},{"B":2}]

Prakash Thapa
  • 1,285
  • 14
  • 27

1 Answers1

1

This is not the cleanest ReQL in the world, but it works!

r.expr([  
   {  
      "group":[  
         0,
         "A"
      ],
      "reduction":14
   },
   {  
      "group":[  
         "B",
         0
      ],
      "reduction":2
   }
])
.map(function (row) {
  // Return a tuple with the key and the value
  return [row('group').filter(function (row) { return row.eq(0).not() }).nth(0), row('reduction') ]
})
.map(function (row) {
  // Convert the tuple into an array by passing an array of key-value pairs
  return r.expr([row]).coerceTo('object')
})

You could combine these two maps functions, but I personally think it's nicer to separate them out, in order to make them more understandable.

I used r.expr so you can just copy/paste it into the data explorer and see if it works, but you can just copy/paste the last two mapsat the end of your query and the result would be the same.

Result:

[
  {
    "A": 14
  } ,
  {
    "B": 2
  }
]
Jorge Silva
  • 4,574
  • 1
  • 23
  • 42