{"id":642,"date":"2023-11-27T09:52:49","date_gmt":"2023-11-27T09:52:49","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=642"},"modified":"2023-11-30T09:17:55","modified_gmt":"2023-11-30T09:17:55","slug":"convert-object-to-array-javascript","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/convert-object-to-array-javascript\/","title":{"rendered":"Convert Object to Array in JavaScript (with code)"},"content":{"rendered":"\n<p>The arrays and objects are the two most important data types in JavaScript. In this article, we will look into various methods to Covert Object to Array in JavaScript. We can store just the keys or just the values(corresponding to the keys) of an object in an array. We can even use a 2D array to store the key-value pairs of the object as a whole. Well, let us get started.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Revise Objects &amp; Arrays in JavaScript<\/strong><\/h2>\n\n\n\n<p>Just like we use hashmaps in C++ that store data in the form of \u201ckey-value\u201d pairs, We use Objects in Javascript to store data in the same way, i.e. in the form of key-value pairs. So, all in all, Objects are non-primitive data types in Javascript that store an unordered collection of key-value pairs. The association between key values is called property, so objects are a collection of properties.<\/p>\n\n\n\n<p>For example, we have defined an object \u201ccar\u201d with various properties below:<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>let car = {\n    name: \"WagonR\",\n    model: 2015,\n    price: 340000\n};\n<\/code><\/pre>\n\n\n\n<p>Several properties such as name, model, and price are defined for the object \u201ccar\u201d. The name, model, and price here indicate the keys, and \u201cWagonR\u201d, 2015 and 340000 are the values corresponding to the related keys.<\/p>\n\n\n\n<p>On the other hand, Arrays are linear data structures that store homogenous data, i.e. same type of data. An array can contain all integers, all characters, or all strings as the elements. An array can not be a combination of integers and characters, or integers or strings. <\/p>\n\n\n\n<p>Let us look at how to declare and use arrays in Javascript.<\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>\/\/declaring an integers array\nlet marks = &#91;98, 75, 62, 82, 97, 53];\n\n\/\/declaring a characters array\nlet grades=&#91;'A', 'B', 'A', '0'];\n\n\/\/declaring a strings array\nlet favtutors=&#91;\"John\", \"Joshua\", \"Alex\", \"Kate\"];\n\n\/\/declaring a boolean array\nlet present=&#91;true, false, false, true];\n<\/code><\/pre>\n\n\n\n<p>In the code above, we have made integers array marks containing all integers, a characters array grade consisting of all elements as characters, a strings array favtutors consisting of all strings, and a boolean present array consisting of all boolean values.<\/p>\n\n\n\n<p>Let&#8217;s now do the conversion of Object to Array using some of the different approaches.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>5 Methods to Convert an Object to Array in JavaScript<\/strong><\/h2>\n\n\n\n<p>In this section, we will look into various methods to convert an object to an array in Javascript. Let us get started!\u00a0<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1) Using for&#8230;in loop<\/strong><\/h3>\n\n\n\n<p>We can use the for\u2026in loop to iterate over the keys of an object and store the keys and values of the object in separate arrays. This is a naive approach to convert object to array in JavaScript.<\/p>\n\n\n\n<p>Let us see an example of the same:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring an object\nlet studentObject={\n    name:'John',\n    rollNumber:12\n};\n\n\/\/printing the object\nconsole.log(studentObject);\n\n\/\/declaring a variable to store object keys\nlet studentKeysArray=[];\n\n\/\/declaring variable to store values corresp. to keys\nlet studentValuesArray=[];\n\n\/\/using for...in loop to traverse the object and store keys in array\nfor(let key in studentObject){\n    \/\/adding the keys in array\n    studentKeysArray.push(key);\n    \/\/adding the values in another array\n    studentValuesArray.push(studentObject[key]);\n}\n\n\/\/printing the keys stored in an array\nconsole.log(studentKeysArray);\n\n\/\/printing the values stored in array\nconsole.log(studentValuesArray);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>{ name: 'John', rollNumber: 12 }\n&#91; 'name', 'rollNumber' ]\n&#91; 'John', 12 ]\n<\/code><\/pre>\n\n\n\n<p>In this example, we have iterated over the keys of the object. We have stored the keys of the object in the array \u201cstudentKeysArray\u201d. We have stored the values corresponding to the keys in a separate array \u201cstudentValuesArray\u201d.&nbsp;<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2) Using for\u2026of loop<\/strong><\/h3>\n\n\n\n<p>Similar, to for&#8230;in, we can use for&#8230;of loop as well. Let us convert an object to array with the help of a for\u2026of loop. Here\u2019s an example of the same:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring an object\nlet studentObject={\n    name:'John',\n    rollNumber:12\n};\n\n\/\/printing the object\nconsole.log(studentObject);\n\n\/\/declaring a variable to store object keys\nlet studentKeysArray=[];\n\n\/\/declaring a variable to store values corresp. to keys\nlet studentValuesArray=[];\n\n\/\/using for...of loop to iterate the object keys and store keys in an array\nfor(let key of Object.keys(studentObject)){\n    \/\/adding the keys in the array\n    studentKeysArray.push(key);\n    \/\/adding the values in another array\n    studentValuesArray.push(studentObject[key]);\n}\n\n\/\/printing the keys stored in an array\nconsole.log(studentKeysArray);\n\n\/\/printing the values stored in array\nconsole.log(studentValuesArray);<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>{ name: 'John', rollNumber: 12 }\n&#91; 'name', 'rollNumber' ]\n&#91; 'John', 12 ]\n<\/code><\/pre>\n\n\n\n<p>In this example, we have iterated over the keys of the object. We have stored the keys of the object in the array \u201cstudentKeysArray\u201d. We have stored the values corresponding to the keys in a separate array \u201cstudentValuesArray\u201d.&nbsp;<\/p>\n\n\n\n<p>Let us see how we can iterate over the values of an object without accessing the keys:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring an object\nlet studentObject={\n    name:'John',\n    rollNumber:12\n};\n\n\/\/printing the object\nconsole.log(studentObject);\n\n\/\/declaring a variable to store values corresp. to keys\nlet studentValuesArray=[];\n\n\/\/using for...of loop to iterate the object values and store values in the array\nfor(let value of Object.values(studentObject)){\n    \/\/adding the values in another array\n    studentValuesArray.push(value);\n}\n\n\/\/printing the values stored in array\nconsole.log(studentValuesArray);<\/pre><\/div>\n\n\n\n<p><strong>Output<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>{ name: 'John', rollNumber: 12 }\n&#91; 'John', 12 ]\n<\/code><\/pre>\n\n\n\n<p>We have obtained the desired result.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3) Using Object.keys()<\/strong><\/h3>\n\n\n\n<p>Keys are typically used as the names of the identifiers to access the object\u2019s properties. <strong>The Object.keys() method allows us to obtain the keys of an object and store them in an array. <\/strong>This method was introduced in ES6 and is supported in modern browsers.<\/p>\n\n\n\n<p>Here\u2019s an example:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring an object\nlet studentObject={\n    name:'John',\n    rollNumber:12\n};\n\n\/\/printing the object\nconsole.log(studentObject);\n\n\/\/declaring a variable to store object keys\nlet studentKeysArray=Object.keys(studentObject);\n\n\/\/printing the keys of the object stored in array\nconsole.log(studentKeysArray)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>{ name: 'John', rollNumber: 12 }\n&#91; 'name', 'rollNumber' ]\n<\/code><\/pre>\n\n\n\n<p>In this example, we are storing the keys of the declared object in an array variable. The keys for the student object are \u201cname\u201d and \u201crollNumber\u201d. The keys are hence stored in an array and we have obtained the desired result.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4) Using Object.values()<\/strong><\/h3>\n\n\n\n<p>Values represent the values corresponding to the keys in an object.<strong> The Object.values() method allows us to obtain the values corresponding to the keys of an object and store them in an array.<\/strong> This method was introduced in ES8 and is supported in modern browsers.<\/p>\n\n\n\n<p>Here\u2019s an example of this method to convert object to array in JavaScript:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring an object\nlet studentObject={\n    name:'John',\n    rollNumber:12\n};\n\n\/\/printing the object\nconsole.log(studentObject);\n\n\/\/declaring a variable to store object values\nlet studentValuesArray=Object.values(studentObject);\n\n\/\/printing the values corresponding keys of the object stored in array\nconsole.log(studentValuesArray)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>{ name: 'John', rollNumber: 12 }\n&#91; 'John', 12 ]\n<\/code><\/pre>\n\n\n\n<p>In this example, we are storing the values corresponding to the keys of the declared object in an array variable. The values for the student object are \u201cJohn\u201d corresponding to the key \u201cname\u201d and \u201c12\u201d corresponding to the key \u201crollNumber\u201d. The values are hence stored in an array and we have obtained the desired result.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>5) Using Object.entries()<\/strong><\/h3>\n\n\n\n<p>The key-value pairs as a whole are called entries. <strong>The Object.entries() method is used to convert the key-value pairs of an object as entities to a 2D array. <\/strong>This method was also introduced in ES8 and is supported in modern browsers.<\/p>\n\n\n\n<p>Here\u2019s an example of this method:<\/p>\n\n\n\n<div class=\"wp-block-codemirror-blocks-code-block code-block\"><pre class=\"CodeMirror\" data-setting=\"{&quot;mode&quot;:&quot;javascript&quot;,&quot;mime&quot;:&quot;text\/javascript&quot;,&quot;theme&quot;:&quot;material&quot;,&quot;lineNumbers&quot;:true,&quot;styleActiveLine&quot;:false,&quot;lineWrapping&quot;:false,&quot;readOnly&quot;:true,&quot;language&quot;:&quot;JavaScript&quot;,&quot;modeName&quot;:&quot;js&quot;}\">\/\/declaring an object\nlet studentObject={\n    name:'John',\n    rollNumber:12\n};\n\n\/\/printing the object\nconsole.log(studentObject);\n\n\/\/declaring a variable to store object entries(key-value pairs)\nlet studentEntriesArray=Object.entries(studentObject);\n\n\/\/printing the key-value pairs of the object stored in an array\nconsole.log(studentEntriesArray)<\/pre><\/div>\n\n\n\n<p><strong>Output:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-code has-black-color has-text-color has-background\" style=\"background-color:#fedcba\"><code>{ name: 'John', rollNumber: 12 }\n&#91; &#91; 'name', 'John' ], &#91; 'rollNumber', 12 ] ]\n<\/code><\/pre>\n\n\n\n<p>In this example, we are storing the key-value pairs of the declared object in a 2D Array variable. The key-value pairs for the student object are \u201cname: John\u201d and \u201crollNumber:12\u201d. The key-value pairs are hence stored in a 2D Array and we have obtained the desired result.<\/p>\n\n\n\n<p>Note that The Object.keys() method is widely supported in all modern browsers, including Internet Explorer 9 and above but the Object.values() and Object.entries() methods have slightly less browser support. <\/p>\n\n\n\n<p>With this array, you may have to get a string from that. Here is how to <a href=\"https:\/\/favtutor.com\/articles\/convert-array-to-string-javascript\/\" data-type=\"link\" data-id=\"https:\/\/favtutor.com\/articles\/convert-array-to-string-javascript\/\">convert an array to a string in JavaScript<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we discussed the various techniques to convert an object to an array in JavaScript. It\u2019s beneficial to know at least some methods as they come in very handy at times. This type of conversion is a critical skill that web developers needs to be aware of, be it object to array, array to string, etc.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Find out different methods to convert objects into arrays in JavaScript using loop, object keys, object entries etc.<\/p>\n","protected":false},"author":9,"featured_media":645,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"jnews-multi-image_gallery":[],"jnews_single_post":null,"jnews_primary_category":{"id":"","hide":""},"footnotes":""},"categories":[9],"tags":[11,13,10],"class_list":["post-642","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-javascript","tag-javascript-array","tag-javascript-object"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/642","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/comments?post=642"}],"version-history":[{"count":4,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/642\/revisions"}],"predecessor-version":[{"id":699,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/642\/revisions\/699"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/645"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=642"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=642"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=642"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}