{"id":1348,"date":"2023-10-12T15:08:00","date_gmt":"2023-10-12T15:08:00","guid":{"rendered":"https:\/\/www.devopsfreelancer.com\/blog\/?p=1348"},"modified":"2023-10-31T15:11:45","modified_gmt":"2023-10-31T15:11:45","slug":"how-to-convert-array-to-object-in-javascript","status":"publish","type":"post","link":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-convert-array-to-object-in-javascript\/","title":{"rendered":"How to Convert Array to Object in Javascript?"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>What is Array Object ?<\/strong><\/h2>\n\n\n\n<p>JavaScript, arrays are a special type of object used for storing and manipulating lists of values. Despite their name, arrays in JavaScript are more flexible than traditional arrays in other languages, as they can hold a mixture of data types and can dynamically resize. You can think of an array in JavaScript as an object with a special structure optimized for handling sequential data.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>There are two main ways to convert an array to an object in JavaScript:<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Using the&nbsp;<code>Object.assign()<\/code>&nbsp;method:<\/strong>&nbsp;The&nbsp;<code class=\"\">Object.assign()<\/code>&nbsp;method takes one or more source objects and copies their properties to a target object.&nbsp;To convert an array to an object using the&nbsp;<code class=\"\">Object.assign()<\/code>&nbsp;method,&nbsp;we can pass the array as the source object and an empty object as the target object.<\/li>\n<\/ol>\n\n\n\n<pre class=\"wp-block-code\"><code>const arr = &#91;1, 2, 3];\r\nconst obj = Object.assign({}, arr);\r\r\nconsole.log(obj); \/\/ {0: 1, 1: 2, 2: 3}\r<\/code><\/pre>\n\n\n\n<p><strong>Using the\u00a0<code>Object.fromEntries()<\/code>\u00a0method:<\/strong>\u00a0The\u00a0<code class=\"\">Object.fromEntries()<\/code>\u00a0method takes an iterable of key-value pairs and returns an object with those properties.\u00a0To convert an array to an object using the\u00a0<code class=\"\">Object.fromEntries()<\/code>\u00a0method,\u00a0we can use the\u00a0<code class=\"\">Array.prototype.map()<\/code>\u00a0method to create an iterable of key-value pairs from the array.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>const arr = &#91;&#91;\"name\", \"John Doe\"], &#91;\"age\", 30]];\r\nconst obj = Object.fromEntries(arr.map((&#91;key, value]) => &#91;key, value]));\r\n\r\nconsole.log(obj); \/\/ {name: \"John Doe\", age: 30}<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>What is Array Object ? JavaScript, arrays are a special type of object used for storing and manipulating lists of values. Despite their name, arrays in JavaScript&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[199],"tags":[22,202,530],"class_list":["post-1348","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-javascript","tag-javascript-array","tag-javascript-object"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How to Convert Array to Object in Javascript? - DevOps Freelancer<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.devopsfreelancer.com\/blog\/how-to-convert-array-to-object-in-javascript\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to Convert Array to Object in Javascript? - DevOps Freelancer\" \/>\n<meta property=\"og:description\" content=\"What is Array Object ? JavaScript, arrays are a special type of object used for storing and manipulating lists of values. Despite their name, arrays in JavaScript...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.devopsfreelancer.com\/blog\/how-to-convert-array-to-object-in-javascript\/\" \/>\n<meta property=\"og:site_name\" content=\"DevOps Freelancer\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/amitsthakurs\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-10-12T15:08:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-10-31T15:11:45+00:00\" \/>\n<meta name=\"author\" content=\"Amit Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/amits_thakurs\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Amit Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-convert-array-to-object-in-javascript\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-convert-array-to-object-in-javascript\\\/\"},\"author\":{\"name\":\"Amit Kumar\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#\\\/schema\\\/person\\\/22ed4bd82dc04200a2ca541b3e35fc5b\"},\"headline\":\"How to Convert Array to Object in Javascript?\",\"datePublished\":\"2023-10-12T15:08:00+00:00\",\"dateModified\":\"2023-10-31T15:11:45+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-convert-array-to-object-in-javascript\\\/\"},\"wordCount\":188,\"commentCount\":1,\"keywords\":[\"javascript\",\"javascript-array\",\"javascript-object\"],\"articleSection\":[\"Javascript\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-convert-array-to-object-in-javascript\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-convert-array-to-object-in-javascript\\\/\",\"url\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-convert-array-to-object-in-javascript\\\/\",\"name\":\"How to Convert Array to Object in Javascript? - DevOps Freelancer\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#website\"},\"datePublished\":\"2023-10-12T15:08:00+00:00\",\"dateModified\":\"2023-10-31T15:11:45+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#\\\/schema\\\/person\\\/22ed4bd82dc04200a2ca541b3e35fc5b\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-convert-array-to-object-in-javascript\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-convert-array-to-object-in-javascript\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/how-to-convert-array-to-object-in-javascript\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to Convert Array to Object in Javascript?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/\",\"name\":\"DevOps Freelancer\",\"description\":\"We provide DevOps | SRE | DevSecOps | MLOps Freelancing\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#\\\/schema\\\/person\\\/22ed4bd82dc04200a2ca541b3e35fc5b\",\"name\":\"Amit Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g\",\"caption\":\"Amit Kumar\"},\"description\":\"Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer\\\/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.\",\"sameAs\":[\"https:\\\/\\\/www.facebook.com\\\/amitsthakurs\\\/\",\"https:\\\/\\\/www.instagram.com\\\/amits_thakurs\\\/\",\"https:\\\/\\\/www.linkedin.com\\\/in\\\/amits-thakurs\\\/\",\"https:\\\/\\\/x.com\\\/https:\\\/\\\/twitter.com\\\/amits_thakurs\"],\"url\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/author\\\/amit\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to Convert Array to Object in Javascript? - DevOps Freelancer","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-convert-array-to-object-in-javascript\/","og_locale":"en_US","og_type":"article","og_title":"How to Convert Array to Object in Javascript? - DevOps Freelancer","og_description":"What is Array Object ? JavaScript, arrays are a special type of object used for storing and manipulating lists of values. Despite their name, arrays in JavaScript...","og_url":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-convert-array-to-object-in-javascript\/","og_site_name":"DevOps Freelancer","article_author":"https:\/\/www.facebook.com\/amitsthakurs\/","article_published_time":"2023-10-12T15:08:00+00:00","article_modified_time":"2023-10-31T15:11:45+00:00","author":"Amit Kumar","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/amits_thakurs","twitter_misc":{"Written by":"Amit Kumar","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-convert-array-to-object-in-javascript\/#article","isPartOf":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-convert-array-to-object-in-javascript\/"},"author":{"name":"Amit Kumar","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b"},"headline":"How to Convert Array to Object in Javascript?","datePublished":"2023-10-12T15:08:00+00:00","dateModified":"2023-10-31T15:11:45+00:00","mainEntityOfPage":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-convert-array-to-object-in-javascript\/"},"wordCount":188,"commentCount":1,"keywords":["javascript","javascript-array","javascript-object"],"articleSection":["Javascript"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.devopsfreelancer.com\/blog\/how-to-convert-array-to-object-in-javascript\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-convert-array-to-object-in-javascript\/","url":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-convert-array-to-object-in-javascript\/","name":"How to Convert Array to Object in Javascript? - DevOps Freelancer","isPartOf":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/#website"},"datePublished":"2023-10-12T15:08:00+00:00","dateModified":"2023-10-31T15:11:45+00:00","author":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b"},"breadcrumb":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-convert-array-to-object-in-javascript\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.devopsfreelancer.com\/blog\/how-to-convert-array-to-object-in-javascript\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.devopsfreelancer.com\/blog\/how-to-convert-array-to-object-in-javascript\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.devopsfreelancer.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to Convert Array to Object in Javascript?"}]},{"@type":"WebSite","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#website","url":"https:\/\/www.devopsfreelancer.com\/blog\/","name":"DevOps Freelancer","description":"We provide DevOps | SRE | DevSecOps | MLOps Freelancing","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.devopsfreelancer.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b","name":"Amit Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g","caption":"Amit Kumar"},"description":"Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer\/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.","sameAs":["https:\/\/www.facebook.com\/amitsthakurs\/","https:\/\/www.instagram.com\/amits_thakurs\/","https:\/\/www.linkedin.com\/in\/amits-thakurs\/","https:\/\/x.com\/https:\/\/twitter.com\/amits_thakurs"],"url":"https:\/\/www.devopsfreelancer.com\/blog\/author\/amit\/"}]}},"_links":{"self":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1348","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/comments?post=1348"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1348\/revisions"}],"predecessor-version":[{"id":1349,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1348\/revisions\/1349"}],"wp:attachment":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/media?parent=1348"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/categories?post=1348"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/tags?post=1348"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}