Languages
[Edit]
EN

JavaScript - iterate over object

3 points
Created by:
Dirask Community
5850

In this article, we would like to show you how to iterate over an object in JavaScript.

Below examples show two ways of how to do that:

  1. Using for...in statement
  2. using Object.entries() method

1. for...in statement example

Below example shows how to iterate over object properties using for...in statement.

Practical example:

// ONLINE-RUNNER:browser;

var object = {'a': 1, 'b': 2, 'c': 3};

for (const prop in object) {
  console.log(prop);
}

2. Object.entries() method example

Below example shows how to iterate over object using Object.entries() to get every key / value pair from object.

// ONLINE-RUNNER:browser;

var object = {'a': 1, 'b': 2, 'c': 3};

for (const [key, value] of Object.entries(object)) {
  console.log(`${key}: ${value}`);
}

References

Native Advertising
­čÜÇ
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

ÔŁĄ´ŞĆ­čĺ╗ ­čÖé

Join