Object Iteration
Objectives
Explain the difference between looping and iteration.
Iterate over arrays with the
for...of
statement.Enumerate an object's properties with the
for...in
statement.
Introduction
When we create a for
loop to loop over an array, we base the loop's condition off of the .length
of the array. This works, but it's a lot of syntactic cruft to remember:
The problem is that we're using a looping construct to perform iteration.
Looping vs. iteration
There's a pretty fine line separating the concepts of looping and iteration, and only the truly pedantic will call you out if you use one in place of the other.
Looping is the process of executing a set of statements repeatedly until a condition is met. It's great for when we want to do something a specific number of times (for
loop) or unlimited times until the condition is met (while
loop).
Iteration is the process of executing a set of statements once for each element in a collection. Prior to ES2015, there were a few ways to do this, but none were very pretty. With a for
loop:
With a while
loop:
for...of
for...of
ES2015 introduced the for...of
statement, and, well, just compare this to the previous two snippets:
It's so much cleaner than the looping solutions — no initialization of a counter, no condition, no incrementing the counter, and no bracket notation to access elements in the array (myArray[i]
).
const
vs. let
const
vs. let
As you might've noticed, for...of
allows us to use const
instead of let
. In for
and while
statements, let
is required because we are incrementing a counter variable. The incrementing process involves taking the counter's current value, adding 1
to it, and then assigning that new value to the variable. That reassignment precludes us from using our beloved const
, which cannot be reassigned.
Delightfully, the for...of
statement involves no such reassignment. On each trip into the loop body (which is a block — note the curly braces), we assign the next element in the collection to a new element
variable. Upon reaching the end of the block, the block-scoped variable vanishes, and we return to the top. Then we repeat the process, assigning the next element in the collection to a new element
variable.
Iterating over... strings?
A string is effectively an ordered collection (an array) of characters, which for...of
is more than happy to iterate over:
Usage
Use a for...of
statement anytime you want to iterate over an array.
Iterating over objects
The for...in
statement has been around for a long time, and it's usually used for iterating over the properties in an object. The statement follows this syntax:
The for...in
statement iterates over the properties in an object, but it doesn't pass the entire property into the block. Instead, it only passes in the keys:
Accessing the object's values is as simple as combining the passed-in key with the bracket operator:
But... but I want to use the dot operator!
Can you think of why the bracket operator is required? Let's see what happens when we use the dot operator:
The for...in
statement iterates over the five properties in address
, successively passing in the object's keys. However, inside the statement body we're trying to access address.key
. If you recall from the lesson on objects, variables don't work with the dot operator because it treats the variable name as a literal key — that is, address.key
is trying to access the property on address
with a key of key
. Since there is no key
property in address
, it returns undefined
. To prove this, let's add a key
property to address
:
Usage
Use a for...in
statement whenever you want to enumerate the properties of an object.
for...in
and order
for...in
and orderA
for...in
loop iterates over the properties of an object in an arbitrary order ... one cannot depend on the seeming orderliness of iteration, at least in a cross-browser setting).
Additionally, in the pre-ES2015 days, using for...in
would often result in different browsers iterating over the same object's properties in different orders. That's not cool! Cross-browser consistency is very important. A lot of progress has been made towards standardizing the behavior of for...in
across all major browsers, but there's still no reason to use for...in
with arrays when we have the wonderfully consistent for...of
tailor-made for the job.
Resources
Last updated