{"id":12782,"date":"2025-07-15T07:21:19","date_gmt":"2025-07-15T07:21:19","guid":{"rendered":"https:\/\/www.bacancytechnology.com\/qanda\/?p=12782"},"modified":"2025-07-15T07:21:53","modified_gmt":"2025-07-15T07:21:53","slug":"javascript-hashmap-equivalent","status":"publish","type":"post","link":"https:\/\/www.bacancytechnology.com\/qanda\/javascript\/javascript-hashmap-equivalent","title":{"rendered":"JavaScript Hashmap Equivalent"},"content":{"rendered":"<p>In JavaScript, the closest equivalents to a HashMap (from other languages like Java or C++) are:<\/p>\n<ul>\n<li><strong>Object \u2013<\/strong> a collection of key\u2013value pairs where keys are strings or symbols.<\/li>\n<li><strong>Map \u2013<\/strong> introduced in ES6, allows keys of any type and maintains insertion order.<\/li>\n<\/ul>\n<h2>Option 1: Using Object (Traditional):<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\r\nconst userDetails = {\r\n name: \"Alice\",\r\n age: 30,\r\n role: \"Developer\"\r\n};\r\n\/\/ Accessing values\r\nconsole.log(userDetails[\"name\"]); \/\/ Alice\r\n\r\n\/\/ Adding a new key\r\nuserDetails[\"location\"] = \"New York\";\r\n\r\n\/\/ Iterating\r\nfor (const key in userDetails) {\r\n console.log(`${key}: ${userDetails[key]}`);\r\n}\r\n<\/pre>\n<h2>Option 2: Using Map (Recommended for most use-cases)<\/h2>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"js\">\r\nconst userMap = new Map();\r\n\/\/ Setting key-value pairs\r\nuserMap.set(\"name\", \"Bob\");\r\nuserMap.set(\"age\", 25);\r\nuserMap.set(\"role\", \"Designer\");\r\n\r\n\/\/ Getting values\r\nconsole.log(userMap.get(\"name\")); \/\/ Bob\r\n\/\/ Checking if a key exists\r\nconsole.log(userMap.has(\"age\")); \/\/ true\r\n\/\/ Iterating\r\nuserMap.forEach((value, key) => {\r\n console.log(`${key}: ${value}`);\r\n});\r\n<\/pre>\n<h3>Summary<\/h3>\n<p><strong>Use Map if:<\/strong><\/p>\n<ul>\n<li>You need keys other than strings.<\/li>\n<li>You care about insertion order.<\/li>\n<li>You want better built-in iteration and performance.<\/li>\n<\/ul>\n<p>&nbsp;<br \/>\n<strong>Use Object if:<\/strong><\/p>\n<ul>\n<li>You only need simple key-value pairs with string keys.<\/li>\n<li>You need to serialize easily using JSON.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>In JavaScript, the closest equivalents to a HashMap (from other languages like Java or C++) are: Object \u2013 a collection of key\u2013value pairs where keys are strings or symbols. Map \u2013 introduced in ES6, allows keys of any type and maintains insertion order. Option 1: Using Object (Traditional): const userDetails = { name: &#8220;Alice&#8221;, age: [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":12783,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[22],"tags":[],"class_list":["post-12782","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-javascript"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12782"}],"collection":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/comments?post=12782"}],"version-history":[{"count":2,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12782\/revisions"}],"predecessor-version":[{"id":12785,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/posts\/12782\/revisions\/12785"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media\/12783"}],"wp:attachment":[{"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/media?parent=12782"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/categories?post=12782"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bacancytechnology.com\/qanda\/wp-json\/wp\/v2\/tags?post=12782"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}