{"id":382,"date":"2019-05-27T09:16:33","date_gmt":"2019-05-27T07:16:33","guid":{"rendered":"http:\/\/www.stepit.co.za\/wp\/?p=382"},"modified":"2019-06-03T09:27:45","modified_gmt":"2019-06-03T07:27:45","slug":"javascript-object-to-array-and-visa-versa","status":"publish","type":"post","link":"http:\/\/www.stepit.co.za\/wp\/blog\/2019\/05\/27\/javascript-object-to-array-and-visa-versa\/","title":{"rendered":"Javascript Object to Array and visa versa"},"content":{"rendered":"<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\nconst obj = {&quot;1&quot;:&quot;5&quot;,&quot;2&quot;:7,&quot;3&quot;:9,&quot;4&quot;:155,&quot;6&quot;:&quot;189&quot;,&quot;7&quot;:587,&quot;8&quot;:&quot;455&quot;,&quot;9&quot;:&quot;89&quot;,&quot;10&quot;:&quot;80&quot;,&quot;12&quot;:&quot;887.6&quot;,&quot;13&quot;:89,&quot;14&quot;:9}\nconst objToArr = Object.keys(obj).map(function(key) {\n  return &#x5B;key, Number(obj&#x5B;key]) ];\n});\nconsole.log(objToArr)\n\/*\n&#x5B; &#x5B; '1', 5 ],\n  &#x5B; '2', 7 ],\n  &#x5B; '3', 9 ],\n  &#x5B; '4', 155 ],\n  &#x5B; '6', 189 ],\n  &#x5B; '7', 587 ],\n  &#x5B; '8', 455 ],\n  &#x5B; '9', 89 ],\n  &#x5B; '10', 80 ],\n  &#x5B; '12', 887.6 ],\n  &#x5B; '13', 89 ],\n  &#x5B; '14', 9 ] ]\n*\/\n\n\nlet resultObj = objToArr.reduce(function(result, item, index, array) {\n   Object.assign(result, { &#x5B; item &#x5B; 0 ] ]:item&#x5B;1]} )\n  return result;\n}, {}) \/\/watch out the empty {}, which is passed as &quot;result&quot;\nconsole.log(resultObj)\n\/*\n{ '1': 5,\n  '2': 7,\n  '3': 9,\n  '4': 155,\n  '6': 189,\n  '7': 587,\n  '8': 455,\n  '9': 89,\n  '10': 80,\n  '12': 887.6,\n  '13': 89,\n  '14': 9 }\n*\/\n\nresultObj2 = {...objToArr};\nconsole.log(resultObj2)\n\/*\n{ '0': &#x5B; '1', 5 ],\n  '1': &#x5B; '2', 7 ],\n  '2': &#x5B; '3', 9 ],\n  '3': &#x5B; '4', 155 ],\n  '4': &#x5B; '6', 189 ],\n  '5': &#x5B; '7', 587 ],\n  '6': &#x5B; '8', 455 ],\n  '7': &#x5B; '9', 89 ],\n  '8': &#x5B; '10', 80 ],\n  '9': &#x5B; '12', 887.6 ],\n  '10': &#x5B; '13', 89 ],\n  '11': &#x5B; '14', 9 ] }\n*\/\n\n\n\n\n<\/pre><\/div>\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: jscript; title: ; notranslate\" title=\"\">\n\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><\/p>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[30],"tags":[],"class_list":["post-382","post","type-post","status-publish","format-standard","hentry","category-javascript"],"_links":{"self":[{"href":"http:\/\/www.stepit.co.za\/wp\/wp-json\/wp\/v2\/posts\/382","targetHints":{"allow":["GET"]}}],"collection":[{"href":"http:\/\/www.stepit.co.za\/wp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.stepit.co.za\/wp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.stepit.co.za\/wp\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.stepit.co.za\/wp\/wp-json\/wp\/v2\/comments?post=382"}],"version-history":[{"count":6,"href":"http:\/\/www.stepit.co.za\/wp\/wp-json\/wp\/v2\/posts\/382\/revisions"}],"predecessor-version":[{"id":388,"href":"http:\/\/www.stepit.co.za\/wp\/wp-json\/wp\/v2\/posts\/382\/revisions\/388"}],"wp:attachment":[{"href":"http:\/\/www.stepit.co.za\/wp\/wp-json\/wp\/v2\/media?parent=382"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.stepit.co.za\/wp\/wp-json\/wp\/v2\/categories?post=382"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.stepit.co.za\/wp\/wp-json\/wp\/v2\/tags?post=382"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}