{"id":4631,"date":"2016-07-04T15:11:42","date_gmt":"2016-07-04T15:11:42","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=4631"},"modified":"2016-07-04T15:11:42","modified_gmt":"2016-07-04T15:11:42","slug":"merge-two-arrays-rethinkdb","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/merge-two-arrays-rethinkdb\/","title":{"rendered":"How to merge two arrays in RethinkDB"},"content":{"rendered":"<p>In RethinkDB, you can concatenate two arrays with the &#8220;add&#8221; function. It seems that this can detect the type of it&#8217;s arguments and behave accordingly, since this is also used for numerical addition.<\/p>\n<p>In my case, I started with a table where each row contained an array:<\/p>\n<pre lang=\"javascript\">\n{\n  id: 1,\n  entities: [\n    {...}, {...}, {...}\n  ]\n},\n{\n  id: 2,\n  entities: [\n    {...}, {...}\n  ]\n}\n<\/pre>\n<p>By design of this application the values in &#8220;entities&#8221; were supposed to be the actual rows in the table, not nested in random groups, so I&#8217;ve written a query to flatten them into one giant array:<\/p>\n<pre lang=\"javascript\">\nr.db('Watson')\n .table('description_s_TextGetRankedNamedEntities')\n .map( \n   function(row) {\n     return row('entities');\n   }\n  )\n  .reduce(\n    function(left, right) {\n      return r.add(left, right)\n    })\n<\/pre>\n<p>The key here is you probably always need a &#8220;map&#8221; before a &#8220;reduce&#8221; so that your reduction step doesn&#8217;t get mixed arguments If you try to combine switching the shape of the entry data with the reduction, the &#8220;reduce&#8221; function would have to detect both left\/right shapes to do the combination correctly, since it is designed to operate in parallel.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>How to merge a lot of arrays into one in RethinkdB<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"footnotes":""},"categories":[4],"tags":[466],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4631"}],"collection":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/comments?post=4631"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4631\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=4631"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=4631"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=4631"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}