{"id":3973,"date":"2016-04-29T03:22:45","date_gmt":"2016-04-29T03:22:45","guid":{"rendered":"http:\/\/www.garysieling.com\/blog\/?p=3973"},"modified":"2016-04-29T03:22:45","modified_gmt":"2016-04-29T03:22:45","slug":"lodash-reverse-map","status":"publish","type":"post","link":"https:\/\/www.garysieling.com\/blog\/lodash-reverse-map\/","title":{"rendered":"Lodash: reverse a map"},"content":{"rendered":"<p>The following code snippet will take a Javascript object, and swap the keys and values. In order to handle duplicates, the result puts values into arrays.<\/p>\n<p>For instance, this object:<\/p>\n<pre lang=\"javascript\">\nlet categories = {\n  \"History\": {\n    \"Political History\": {},\n    \"20th century history\": {}\n  },\n  \"Politics\": {\n    \"Political History\": {},\n    \"American Politics\": {}\n  }\n}\n<\/pre>\n<p>Turns into this:<\/p>\n<pre lang=\"javascript\">\n{\n  \"Political History\": [\"History\", \"Politics\"],\n  \"20th century history\": [\"History\"],\n  \"American Politics\": [\"Politics\"]\n}\n<\/pre>\n<p>Code example:<\/p>\n<pre lang=\"javascript\">\n_.mapValues(\n  _.groupBy(\n    _.flatMap(\n      _.keys(categories), \n      k1 => \n        _.map(\n          _.keys(categories[k1]), \n          k2 => [k2, k1] \n        ) \n      ), \n  tuple => tuple[0]), \n  (v) => \n    _.flatMap(v, \n      v => _.filter(\n             v, \n             (value, idx) => idx % 2 == 1)\n    )\n)\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>A handy javascript snippet to reverse an object<\/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,343],"aioseo_notices":[],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3973"}],"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=3973"}],"version-history":[{"count":0,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/posts\/3973\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/media?parent=3973"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/categories?post=3973"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.garysieling.com\/blog\/wp-json\/wp\/v2\/tags?post=3973"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}