JS Coding Questions Logo
JS Coding Questions
#127💼 Interview💻 Code

How do you loop through or enumerate javascript object

Advertisement

728x90

You can use the for-in loop to loop through javascript object. You can also make sure that the key you get is an actual property of an object, and doesn't come from the prototype using hasOwnProperty method.

javascript
1var object = {
2  k1: "value1",
3  k2: "value2",
4  k3: "value3",
5  };
6
7for (var key in object) {
8  if (object.hasOwnProperty(key)) {
9  console.log(key + " -> " + object[key]); // k1 -> value1 ...
10  }
11}

Advertisement

Responsive Ad
🎯 Practice NowRelated Challenge

JavaScript Coding Exercise 43

Test your knowledge with this interactive coding challenge.

Start Coding

Advertisement

728x90
127of476
How do you loop through or enumerate javascript object | JSCodingQuestions.com