Iterator is a unified interface, also called walker
it is the role of the various data structures can be easy to access, it is through a key way to achieve for the Symbol.iterator
Define an array:
const people = ['Tom', 'Jerry', 'Mario', 'Yoshi'];
Print it in the console, you will find that it has such a property:
can conduct such operations:
const people = ['Tom', 'Jerry', 'Mario', 'Yoshi'];
const it = people[Symbol.iterator]();
There are two parameters, when traversing in the end, done will become true
Iterative data structure may be
The main collection of objects can be iterated mainly into three categories: Array, Map, Set
ordinary objects can not be iterative
Array of entries () method
An array has entries () method which returns an iterator:
Then you can use the next methods:
It is noted that: its return value is an array, each including index and attribute values
Array of keys () method
keys () method returns an iterator is, and its value is an index:
Array of values () method
values () method also returns an iterator, its value is the property value: