Looping through arrays in javascript

Home /

Table of Contents

About traversing through object in JavaScript

Traversing through an object in JavaScript refers to iterating over the properties of an object and performing some action on each property.

In JavaScript, objects are collections of key-value pairs, where the keys are strings and the values can be any type of data. When traversing through an object, you can access each key-value pair and perform some action on the value, such as displaying it to the console, adding it to an array, or modifying it.

There are several ways to traverse through an object in JavaScript, such as using a for-in loop, Object.keys(), Object.values(), Object.entries(), Object.getOwnPropertyNames(), and for-of loop. Each method has its own advantages and disadvantages, and the choice of which one to use depends on the specific use case.

For example, a for-in loop allows you to iterate over the enumerable properties of an object, including its own properties and those inherited from its prototype, while Object.keys() method returns an array of a given object’s own property names, in the same order as we get with a normal loop.

It’s important to note that when traversing through an object, the order of properties is not guaranteed and can change based on the browser, JavaScript engine, and implementation.

How to traverse through object

In JavaScript, you can use a for-in loop to traverse through the properties of an object. The for-in loop allows you to iterate over the enumerable properties of an object, including its own properties and those inherited from its prototype.

Here’s an example of using a for-in loop to iterate through the properties of an object:

JavaScript
let obj = {a:1, b:2, c:3};

for (let prop in obj) {
  console.log(prop + ": " + obj[prop]);
}

This will output the following to the console:

a: 1
b: 2
c: 3

You can also use Object.keys() method to get the properties of an object and then use the standard for loop to iterate over it

JavaScript
let obj = {a:1, b:2, c:3};
let keys = Object.keys(obj);
for (let i = 0; i < keys.length; i++) {
  console.log(keys[i] + ": " + obj[keys[i]]);
}

This will give the same output as above.

It’s worth noting that for-in loop will also iterate over any enumerable properties on the object’s prototype chain, which may not be desired. To avoid this, you can use the hasOwnProperty() method to check if a property belongs to the object itself or if it is inherited.

JavaScript
for (let prop in obj) {
  if (obj.hasOwnProperty(prop)) {
    console.log(prop + ": " + obj[prop]);
  }
}

Also, if you want to traverse through an array, you can use for-of loop which is introduced in ES6, it allows you to iterate over the elements of an iterable such as an array, set or map.

JavaScript
let arr = [1,2,3,4];
for(let i of arr) {
  console.log(i);
}

This will output the elements of the array one by one.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.

Other Recommended Article