{"id":999,"date":"2023-12-15T13:00:00","date_gmt":"2023-12-15T13:00:00","guid":{"rendered":"https:\/\/favtutor.com\/articles\/?p=999"},"modified":"2023-12-16T09:23:33","modified_gmt":"2023-12-16T09:23:33","slug":"loop-through-object-javascript","status":"publish","type":"post","link":"https:\/\/favtutor.com\/articles\/loop-through-object-javascript\/","title":{"rendered":"Loop through an Object in JavaScript (4 Methods)"},"content":{"rendered":"\n<p>Objects are the most important data types in JS. This article will look into various methods to loop through an object in JavaScript. Looping is essential to perform various operations such as adding the values of an object, or to find the max and min value, etc. Well, let us get started by revisiting objects and digging deeper into how to iterate over them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>What are Objects in JavaScript?<\/strong><\/h2>\n\n\n\n<p>Before moving forward, we should know what Objects are and how they behave.\u00a0<\/p>\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.<\/p>\n\n\n\n<p>So, 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<h2 class=\"wp-block-heading\"><strong>4 Ways to Loop through JavaScript Object<\/strong><\/h2>\n\n\n\n<p>Looping through objects allows developers to access and manipulate key-value pairs within an object. It is useful for tasks such as data processing, dynamic property access, and object inspection. This process is particularly useful for iterating over an object&#8217;s properties and performing actions based on their values. <\/p>\n\n\n\n<p>Let us get started with various methods to loop through an object in JS:<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>1) Using for&#8230;in loop<\/strong><\/h3>\n\n\n\n<p><strong>The simplest way to loop through an object in JavaScript is by using the for\u2026in loop.<\/strong> This loop is commonly used when we want to perform operations on all properties of an object.<\/p>\n\n\n\n<p>Let us understand how to iterate over an object using a for-in loop in JS:<\/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\/\/using for...in loop to iterate over the object and print the keys\nfor(let key in studentObject){\n    \/\/printing the keys\n    console.log(key);\n}<\/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\nrollNumber\n<\/code><\/pre>\n\n\n\n<p>In this example, we have iterated over the keys of the object and printed them. We have got the desired result.\u00a0<\/p>\n\n\n\n<p>However, note that the for\u2026in loop is that it does not guarantee a specific order of iteration. The order in which properties are traversed may not correspond to the order in which they were added to the object.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>2) 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. The Object.keys() method allows us to obtain the keys of an object and we can iterate over them with the help of for\u2026of loop. This method was introduced in ES6 and is supported in modern browsers. We will also use a for\u2026of loop along with it.<\/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\/\/iterating using the Object.keys() and the for...of loop\nfor(let key of Object.keys(studentObject)){\n    \/\/printing the key\n    console.log(key);\n}<\/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\nrollNumber\n<\/code><\/pre>\n\n\n\n<p>In this example, we have iterated over the keys of the object and we have printed the corresponding keys. We have obtained the desired result.<\/p>\n\n\n\n<p>We have a tutorial to <a href=\"https:\/\/favtutor.com\/articles\/check-if-key-exists-in-javascript-object\/\" data-type=\"link\" data-id=\"https:\/\/favtutor.com\/articles\/check-if-key-exists-in-javascript-object\/\">check if the key exists in the JS object<\/a>, which you might do before using this method.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>3) Using Object.values()<\/strong><\/h3>\n\n\n\n<p>Values represent the values corresponding to the keys in an object. The Object.values() method allows us to obtain the values corresponding to the keys of an object and iterate over them using the for\u2026of loop. This method was introduced in ES8 and is supported in modern browsers.<\/p>\n\n\n\n<p>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\/\/iterating using the Object.values() and for...of loop\nfor(let value of Object.values(studentObject)){\n    \/\/printing the value\n    console.log(value);\n}<\/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>John\n12\n<\/code><\/pre>\n\n\n\n<p>In this example, we have iterated over the values of the object and we have printed the values corresponding to the keys.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>4) Using Object.entries()<\/strong><\/h3>\n\n\n\n<p>The key-value pairs as a whole are called entries. The Object.entries() method is used to convert the key-value pairs of an object as entities to a 2D array, so we try to iterate over that 2D array using a for\u2026of loop. This method was also introduced in ES8 and is supported in modern browsers.<\/p>\n\n\n\n<p>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\/\/iterating using the Object.entries() and for...of loop\nfor(let entry of Object.entries(studentObject)){\n    \/\/printing the key and value\n    console.log(entry[0]+' '+ entry[1]);\n}<\/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\nrollNumber 12\n<\/code><\/pre>\n\n\n\n<p>In this example, we iterated over each entry in the object and correspondingly printed the key-value pairs. 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<h2 class=\"wp-block-heading\"><strong>Conclusion<\/strong><\/h2>\n\n\n\n<p>In this article, we have discussed various techniques to loop through an object in JavaScript. It\u2019s beneficial to know at least these basic methods as they are used very frequently when working on a Project.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We discussed various methods to loop through an object in javascript including for&#8230;in loop and object.keys() method with examples.<\/p>\n","protected":false},"author":9,"featured_media":1002,"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,10],"class_list":["post-999","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-web-development","tag-javascript","tag-javascript-object"],"_links":{"self":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/999","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=999"}],"version-history":[{"count":2,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/999\/revisions"}],"predecessor-version":[{"id":1067,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/posts\/999\/revisions\/1067"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media\/1002"}],"wp:attachment":[{"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/media?parent=999"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/categories?post=999"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/favtutor.com\/articles\/wp-json\/wp\/v2\/tags?post=999"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}