{"id":6432,"date":"2019-06-02T17:41:06","date_gmt":"2019-06-02T21:41:06","guid":{"rendered":"https:\/\/www.sporks.org\/blog\/?p=6432"},"modified":"2019-06-02T17:41:06","modified_gmt":"2019-06-02T21:41:06","slug":"switching-keys-and-values-in-javascript","status":"publish","type":"post","link":"https:\/\/www.sporks.org\/blog\/?p=6432","title":{"rendered":"Switching Keys and Values in JavaScript"},"content":{"rendered":"<p><a href=\"https:\/\/obscurejavascript.tumblr.com\/post\/185321443369\/switching-keys-and-values-in-javascript\" class=\"tumblr_blog\">obscurejavascript<\/a>:<\/p>\n<blockquote>\n<p>A simple invert function can do this. Here is how it works:<\/p>\n<pre><code>const obj = { a: 1, b: 2, c: 3 };\nconsole.log(invert(obj));\n\/\/ { '1': 'a', '2': 'b', '3': 'c' }\n<\/code><\/pre>\n<p>This is useful when there is a list of names to ids. Sometimes you will need to check if an id exists in that list. If this is done in a loop, it is usually much faster to check an inverted id to name object that is created before the loop:<\/p>\n<pre><code>function hasIds(obj, ids) {\n  const byId = invert(obj);\n  return ids.map((id) =&gt; byId[id] !== undefined);\n}\n\nconst cars = { \n  'miata': 1,\n  'elise': 2,\n  'elan': 3\n};\n\nconsole.log(hasIds(cars, [1, 2, 5]));\n\/\/ [ true, true, false ]\n<\/code><\/pre>\n<p>The implementation for <code>invert<\/code> is simple:<\/p>\n<pre><code>function invert(obj) {\n  const newObj = {};\n\n  for (const keyName in obj) {\n    if (obj.hasOwnProperty(keyName)) {\n      newObj[obj[keyName]] = keyName;\n    }\n  }\n\n  return newObj;\n}\n<\/code><\/pre>\n<p>It is also available in many utility libraries <a href=\"https:\/\/lodash.com\/docs\/4.17.11#invert\">like Lodash<\/a>.<\/p>\n<p><strong>Github Location:<\/strong> <a href=\"https:\/\/github.com\/Jacob-Friesen\/obscurejs\/blob\/master\/2019\/invert.js\">https:\/\/github.com\/Jacob-Friesen\/obscurejs\/blob\/master\/2019\/invert.js<\/a><\/p>\n<\/blockquote>\n","protected":false},"excerpt":{"rendered":"<p>obscurejavascript: A simple invert function can do this. Here is how it works: const obj = { a: 1, b: 2, c: 3 }; console.log(invert(obj)); \/\/ { &#8216;1&#8217;: &#8216;a&#8217;, &#8216;2&#8217;: &#8216;b&#8217;, &#8216;3&#8217;: &#8216;c&#8217; } This is useful when there is a list of names to ids. Sometimes you will need to check if an id [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[402,403],"class_list":["post-6432","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-ifttt","tag-tumblr"],"_links":{"self":[{"href":"https:\/\/www.sporks.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6432","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.sporks.org\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sporks.org\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sporks.org\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sporks.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=6432"}],"version-history":[{"count":1,"href":"https:\/\/www.sporks.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6432\/revisions"}],"predecessor-version":[{"id":6433,"href":"https:\/\/www.sporks.org\/blog\/index.php?rest_route=\/wp\/v2\/posts\/6432\/revisions\/6433"}],"wp:attachment":[{"href":"https:\/\/www.sporks.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=6432"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sporks.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=6432"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sporks.org\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=6432"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}