Skip to content

Commit cd53ecb

Browse files
Added new post "Iterating over Object properties in JavaScript"
1 parent d35bc44 commit cd53ecb

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
---
2+
layout: post
3+
title: "Iterating over Object properties in JavaScript"
4+
date: 2020-04-13 20:00:00 -0700
5+
tags: [JavaScript, Object]
6+
comments: true
7+
---
8+
9+
There are several ways to iterate over an object's properties in JavaScript.
10+
11+
[Object.entries()][Object.entries()] returns an array of object's own enumerable string-keyed property `[key, value]` pairs. This array can then be iterated over using [forEach()][Array.prototype.forEach()] or [for...of][for...of].
12+
13+
## Example
14+
15+
{% highlight javascript %}
16+
17+
const book = {
18+
"title": "The Fountainhead",
19+
"author": "Ayn Rand",
20+
"pages": 753
21+
};
22+
23+
Object.entries(book).forEach(([key, value]) => {
24+
console.log(`${key}: ${value}`);
25+
});
26+
27+
{% endhighlight %}
28+
29+
## Output
30+
31+
{% highlight javascript %}
32+
33+
title: The Fountainhead
34+
author: Ayn Rand
35+
pages: 753
36+
37+
{% endhighlight %}
38+
39+
Similarly, `for...of` can also be used with `Object.entries()`.
40+
41+
{% highlight javascript %}
42+
43+
for (let [key, value] of Object.entries(book)) {
44+
console.log(`${key}: ${value}`);
45+
}
46+
47+
{% endhighlight %}
48+
49+
## for...in
50+
51+
`for...in` statement can also be used to iterate over object properties, but it also iterates over inherited properties. Luckily, [hasOwnProperty()][hasOwnProperty()] method can be used to check if a property is object's own property.
52+
53+
{% highlight javascript %}
54+
55+
const book = {
56+
"title": "The Fountainhead",
57+
"author": "Ayn Rand",
58+
"pages": 753
59+
};
60+
61+
for (let key in book) {
62+
if (book.hasOwnProperty(key)) {
63+
console.log(`${key}: ${book[key]}`);
64+
}
65+
}
66+
67+
{% endhighlight %}
68+
69+
## Object.keys() and Object.values()
70+
71+
If we are only concerned with object property keys, then `Object.keys()` can be used. Similarly, `Object.values()` can be used to get object property values.
72+
73+
{% highlight javascript %}
74+
75+
const book = {
76+
"title": "The Fountainhead",
77+
"author": "Ayn Rand",
78+
"pages": 753
79+
};
80+
81+
Object.keys(book).forEach(key => {
82+
console.log(key);
83+
});
84+
85+
Object.values(book).forEach(value => {
86+
console.log(value);
87+
});
88+
89+
{% endhighlight %}
90+
91+
92+
[Object.entries()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/entries
93+
{:target="_blank"}
94+
[Array.prototype.forEach()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach
95+
{:target="_blank"}
96+
[for...of]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
97+
{:target="_blank"}
98+
[hasOwnProperty()]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/hasOwnProperty
99+
{:target="_blank"}

0 commit comments

Comments
 (0)