{"id":913,"date":"2015-06-11T06:49:14","date_gmt":"2015-06-11T06:49:14","guid":{"rendered":"http:\/\/codethataint.com\/blog\/?p=913"},"modified":"2015-06-11T06:49:14","modified_gmt":"2015-06-11T06:49:14","slug":"working-with-keyvalue-pairs-javascript","status":"publish","type":"post","link":"https:\/\/codethataint.com\/blog\/working-with-keyvalue-pairs-javascript\/","title":{"rendered":"Working with Key\/Value Pairs Javascript"},"content":{"rendered":"<p><strong>How can I add Key\/Value pair to Object<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar obj = {\r\n    key1: value1,\r\n    key2: value2\r\n};\r\n<\/pre>\n<p>There are two ways to add new properties to an object<\/p>\n<p><strong>Method 1 &#8211; Using dot notation<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n obj.key3 = &quot;value3&quot;;\r\n<\/pre>\n<p><strong>Method 2 &#8211; Using square bracket notation<\/strong><\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\n obj&#x5B;&quot;key3&quot;] = &quot;value3&quot;;\r\n<\/pre>\n<p>The first form is used when you know the name of the property. The second form is used when the name of the property is dynamically determined. Like in this example:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar getProperty = function (propertyName) {\r\n    return obj&#x5B;propertyName];\r\n};\r\n\r\ngetProperty(&quot;key1&quot;);\r\ngetProperty(&quot;key2&quot;);\r\ngetProperty(&quot;key3&quot;);\r\n<\/pre>\n<p>A real JavaScript array can be constructed using either:<\/p>\n<p>The Array literal notation:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar arr = &#x5B;];\r\n<\/pre>\n<p>The Array constructor notation:<\/p>\n<pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\r\nvar arr = new Array();\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>How can I add Key\/Value pair to Object var obj = { key1: value1, key2: value2 }; There are two ways to add new properties to an object Method 1 &#8211; Using dot notation obj.key3 = &quot;value3&quot;; Method 2 &#8211; Using square bracket notation obj&#x5B;&quot;key3&quot;] = &quot;value3&quot;; The first form is used when you know&hellip; <a href=\"https:\/\/codethataint.com\/blog\/working-with-keyvalue-pairs-javascript\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[119],"tags":[],"class_list":["post-913","post","type-post","status-publish","format-standard","hentry","category-arrays-javascript"],"_links":{"self":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/913","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/comments?post=913"}],"version-history":[{"count":1,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/913\/revisions"}],"predecessor-version":[{"id":914,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/posts\/913\/revisions\/914"}],"wp:attachment":[{"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/media?parent=913"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/categories?post=913"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/codethataint.com\/blog\/wp-json\/wp\/v2\/tags?post=913"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}