ES6: Iterator iterator

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:
Here Insert Picture Description
can conduct such operations:

const people = ['Tom', 'Jerry', 'Mario', 'Yoshi'];
const it = people[Symbol.iterator]();

Here Insert Picture Description
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:
Here Insert Picture Description
Then you can use the next methods:
Here Insert Picture Description
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:
Here Insert Picture Description

Array of values ​​() method

values () method also returns an iterator, its value is the property value:
Here Insert Picture Description

Published 26 original articles · won praise 0 · Views 595

Guess you like

Origin blog.csdn.net/weixin_43856797/article/details/104076207