{"id":4789,"date":"2016-07-26T03:03:21","date_gmt":"2016-07-26T03:03:21","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=4789"},"modified":"2016-07-26T03:03:21","modified_gmt":"2016-07-26T03:03:21","slug":"lodash-flatten-tuples","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/lodash-flatten-tuples\/","title":{"rendered":"lodash: flatten tuples"},"content":{"rendered":"<p>For an object like this:<\/p>\n<pre lang=\"javascript\">\n\nconst type_tags = {\n  'Academic': ['science'],\n  'Tech Conference': [],\n  'Documentary': ['documentary'],\n  'Historical': []\n};\n<\/pre>\n<p>You may want to flatten this in the reverse, which requires the flattening of a list of tuples.<\/p>\n<p>To do this, you first need to create the tuples, reverse them, and flatten them, like so:<\/p>\n<pre lang=\"javascript\">\n_.fromPairs(\n  _.flatMap(\n    type_tags, \n    (k, v) => \n      k.map( \n        key => [key, v] \n      )\n  )\n)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Flattening tuples with lodash<\/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":[302],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4789"}],"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=4789"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/4789\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=4789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=4789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=4789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}