{"id":546,"date":"2018-08-26T08:22:42","date_gmt":"2018-08-26T15:22:42","guid":{"rendered":"https:\/\/actionsnippet.com\/?p=3307"},"modified":"2018-08-26T08:22:42","modified_gmt":"2018-08-26T15:22:42","slug":"dictionary-with-es6-symbol","status":"publish","type":"post","link":"https:\/\/actionsnippet.com\/?p=546","title":{"rendered":"Dictionary with ES6 Symbol"},"content":{"rendered":"<p>Creating a dictionary type object with ES6 Symbols is easy. Yes we have Maps and WeakMaps but this is still interesting for a variety of reasons&#8230; Being able to use objects as keys in another object (dictionary) has many great uses&#8230;. So how do you use Symbols like this?<\/p>\n<pre lang=\"javascript\" line=\"1\">\r\nlet a = { id: Symbol('key') },\r\n    b = { id: Symbol('key') };\r\n\r\nlet dictionary = {\r\n  [a.id]: 'value by obj a',\r\n  [b.id]: 'value by obj b'\r\n};\r\n\r\nconsole.log(dictionary[a.id]);\r\nconsole.log(dictionary[b.id]);\r\n\r\n\/\/ outputs:\r\n\/\/ 'value by obj a'\r\n\/\/ 'value by obj b'\r\n<\/pre>\n<p>By using either object a or object b&#8217;s `id` symbol, our dictionary points to another value. This old AS3 snippet is similar:<\/p>\n<p><a href=\"https:\/\/actionsnippet.com\/?p=426\" target=\"blank\">https:\/\/actionsnippet.com\/?p=426<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Creating a dictionary type object with ES6 Symbols is easy. Yes we have Maps and WeakMaps but this is still interesting for a variety of reasons&#8230; Being able to use objects as keys in another object (dictionary) has many great uses&#8230;. So how do you use Symbols like this? let a = { id: Symbol(&#8216;key&#8217;) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[19,32,104,192,161,160],"tags":[193,173,134,194],"class_list":["post-546","post","type-post","status-publish","format-standard","hentry","category-arrays","category-associative-arrays","category-dictionary","category-es6","category-html5","category-javascript","tag-dictionary","tag-es6","tag-javascript","tag-symbols"],"_links":{"self":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/546","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=546"}],"version-history":[{"count":0,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=\/wp\/v2\/posts\/546\/revisions"}],"wp:attachment":[{"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=546"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=546"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/actionsnippet.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=546"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}