search
HomeWeb Front-endJS TutorialIterator is a unified interface method for accessing data collections.

Iterator is a unified interface method for accessing data collections.

Jul 20, 2018 am 10:15 AM
es6iteratorjavascriptfront endinterface

This article introduces you to the unified interface method of Iterator accessing data collections. It has certain reference value. Friends in need can refer to it.

Introduction

TraverserIterator is a unified interface provided by ES6 for accessing data collections. For any data collection that has a traverser interface deployed internally, users can obtain the corresponding data structure in the same way. If you are using the latest version of the Chrome browser, then you need to know that the Miss Argument we are familiar with has quietly opened another path that can reach her heart.

1 Main topic

A certain data collection deploys the Iterator interface, which means that its Symbol.iterator attribute points to an interface that can return Iterator Interface functions. Any method that uses a traverser to access a data collection by default will call this property to get the traverser object, and then access the members of the data structure in the set order (see the last section for Symbol.iterator further reading). For example, the iterator of a native array is [][Symbol.iterator], or you can directly obtain Array.prototype[Symbol.iterator] through the prototype of its constructor.

1.1 Basic behavior

Calling the Iterator interface will return a new iterator object (pointer object).
There must be a next method in the object, which is used to access the next data member. The pointer initially points to the beginning of the current data structure.

The first time the object's next method is called, the pointer points to the first member of the data structure.
The second time the next method of the object is called, the pointer points to the second member of the data structure.
Continuously call the next method of the object until it points to the end of the data structure.

Every time the next method is called, the same data structure will be returned: { value, done }.
Where value represents the value currently pointing to the member, if not, it is undefined.
Where done is a Boolean value, indicating whether the traversal has ended. The end is true, otherwise false.

The standard of the traverser interface is very simple and does not provide methods such as operating internal pointers, determining whether there is a value, etc. Just keep calling the next method, and when done is false, get the current value, done Just stop when it is true. The first time I came into contact with the behavior pattern of the traverser was in the winter of 2016. At that time, I didn’t have enough background to understand the applicability and power of simplicity. It wasn't until now - just before I was about to pack up and be forced to leave the company that I suddenly woke up. What a painful realization.

let iterator = [1, 2, 3][Symbol.iterator]();

console.log( iterator.next() ); // {value: 1, done: false}
console.log( iterator.next() ); // {value: 2, done: false}
console.log( iterator.next() ); // {value: 3, done: false}
console.log( iterator.next() ); // {value: undefined, done: true}

1.2 Simple implementation

There are different traverser implementation methods for different data structures. We simply implement the array traverser method.

let res = null;
let iterator = myIterator([3, 7]);

console.log( iterator.next() ); // {value: 3, done: false}
console.log( iterator.next() ); // {value: 7, done: false}
console.log( iterator.next() ); // {value: undefined, done: true}

function myIterator(array = []) {
  let index = 0;
  return {
    next() {
      return index <h3 id="return-amp-throw">1.3 return & throw</h3><p>In addition to deploying the <code>next</code> method for the traverser object, there can also be <code>return</code> and <code>throw</code> method. The <code>return</code> method will be called when the <code>for of</code> loop is exited early (usually because of an error, or the <code>break</code> statement is triggered). The <code>throw</code> method is mainly used in conjunction with the <code>Generator</code> function. This method is not used by general traverser objects, so it will not be introduced. </p><pre class="brush:php;toolbar:false">let obj = {
  [Symbol.iterator]() {
    let index = 0;
    let array = [1, 2, 3];

    return {
      next() {
        return index <h2 id="Native-support">2 Native support</h2><h3 id="The-default-holding-traverser">2.1 The default holding traverser</h3><p>The data structures of the native default holding traverser interface are: <br>Basic type: <code>Array </code>, <code>Set</code>, <code>Map</code> (four basic data collections: <code>Array</code>, <code>Object</code>, <code>Set</code> and <code>Map</code>). <br>Array-like objects: <code>arguments</code>, <code>NodeList</code>, <code>String</code>. </p><pre class="brush:php;toolbar:false">let iterator = '123'[Symbol.iterator]();

console.log( iterator.next() ); // {value: "1", done: false}
console.log( iterator.next() ); // {value: "2", done: false}
console.log( iterator.next() ); // {value: "3", done: false}
console.log( iterator.next() ); // {value: undefined, done: true}

Traversers and previous traversal methods
Just because a data collection has a traverser interface does not mean that all methods that traverse it use this interface. In fact, only a few new methods and certain methods added in ES6 will be used, which will be introduced below. For arrays, using for and for of can access the same members, but the actual operations are different.

// 改变数组默认的遍历器接口。
Array.prototype[Symbol.iterator] = function () {
  let index = 0;
  let array = this;

  console.log('Use iterator');

  return {
    next() {
      return index  {
  console.log(d); // 打印出 1, 2。
});

The object does not have a default traverser interface
Why does the object not have a default traverser interface? This should be explained from two aspects. First, the traverser is a linear processing structure. For any non-linear data structure, deploying the traverser interface is equivalent to deploying a linear transformation. Second, the object is originally an unordered collection. If you want it to be ordered, you can use Map instead. This means that everyone has his own strengths and everyone has his own duties. If the dung beetle doesn't roll the dung ball and goes to collect honey, then, well, the flower girl may suffer.

自行生成的类数组对象(拥有length属性),不具备遍历器接口。这与String等原生类数组对象不同,毕竟人家是亲生的,一出生就含着金钥匙(也不怕误吞)。不过我们可以将数组的遍历器接口直接应用于自行生成的类数组对象,简单有效无副作用。

let obj = {
  0: 'a',
  1: 'b',
  length: 2,
  [Symbol.iterator]: Array.prototype[Symbol.iterator]
};

let iterator = obj[Symbol.iterator]();
console.log( iterator.next() ); // {value: "a", done: false}
console.log( iterator.next() ); // {value: "b", done: false}
console.log( iterator.next() ); // {value: undefined, done: true}

为对象添加遍历器接口,也不影响之前不使用遍历器的方法,比如for in, Object.keys等等(两者不等同)。

let obj = {
  0: 'a',
  1: 'b',
  length: 2,
  [Symbol.iterator]: Array.prototype[Symbol.iterator]
};

console.log( Object.keys(obj) ); // ["0", "1", "length"]

for (let v of obj) {
  console.log(v); // 依次打印出:"a", "b"。
}

for (let k in obj) {
  console.log(k); // 依次打印出:"0", "1", "length"。
}

2.2 默认调用遍历器

for of  
for of是专门用来消费遍历器的,其遍历的是键值(for in遍历的是键名)。

for (let v of [1, 2, 3])  {
  console.log(v); // 依次打印出:1, 2, 3。
}

扩展运算符  
无论是解构赋值或扩展运算都是默认调用遍历器的。

let [...a] = [3, 2, 1]; // [3, 2, 1]
let b = [...[3, 2, 1]]; // [3, 2, 1]

yield*  
Generator函数中有yield*命令,如果其后面跟的是一个可遍历的结构,它会调用该结构的遍历器接口。

for (let v of G()) {
  console.log(v); // 依次打印出:1, 2, 3, 4, 5
}

function* G() {
  yield 1;
  yield* [2,3,4];
  yield 5;
}

其它场合  
有些接受数组作为参数的函数,会默认使用数组的遍历器接口,所以也等同于默认调用。比如Array.from(), Promise.all()

相关推荐:

angularjs关于页面模板清除的使用方法

在JS中用slice封装数组方法

The above is the detailed content of Iterator is a unified interface method for accessing data collections.. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Javascript Data Types : Is there any difference between Browser and NodeJs?Javascript Data Types : Is there any difference between Browser and NodeJs?May 14, 2025 am 12:15 AM

JavaScript core data types are consistent in browsers and Node.js, but are handled differently from the extra types. 1) The global object is window in the browser and global in Node.js. 2) Node.js' unique Buffer object, used to process binary data. 3) There are also differences in performance and time processing, and the code needs to be adjusted according to the environment.

JavaScript Comments: A Guide to Using // and /* */JavaScript Comments: A Guide to Using // and /* */May 13, 2025 pm 03:49 PM

JavaScriptusestwotypesofcomments:single-line(//)andmulti-line(//).1)Use//forquicknotesorsingle-lineexplanations.2)Use//forlongerexplanationsorcommentingoutblocksofcode.Commentsshouldexplainthe'why',notthe'what',andbeplacedabovetherelevantcodeforclari

Python vs. JavaScript: A Comparative Analysis for DevelopersPython vs. JavaScript: A Comparative Analysis for DevelopersMay 09, 2025 am 12:22 AM

The main difference between Python and JavaScript is the type system and application scenarios. 1. Python uses dynamic types, suitable for scientific computing and data analysis. 2. JavaScript adopts weak types and is widely used in front-end and full-stack development. The two have their own advantages in asynchronous programming and performance optimization, and should be decided according to project requirements when choosing.

Python vs. JavaScript: Choosing the Right Tool for the JobPython vs. JavaScript: Choosing the Right Tool for the JobMay 08, 2025 am 12:10 AM

Whether to choose Python or JavaScript depends on the project type: 1) Choose Python for data science and automation tasks; 2) Choose JavaScript for front-end and full-stack development. Python is favored for its powerful library in data processing and automation, while JavaScript is indispensable for its advantages in web interaction and full-stack development.

Python and JavaScript: Understanding the Strengths of EachPython and JavaScript: Understanding the Strengths of EachMay 06, 2025 am 12:15 AM

Python and JavaScript each have their own advantages, and the choice depends on project needs and personal preferences. 1. Python is easy to learn, with concise syntax, suitable for data science and back-end development, but has a slow execution speed. 2. JavaScript is everywhere in front-end development and has strong asynchronous programming capabilities. Node.js makes it suitable for full-stack development, but the syntax may be complex and error-prone.

JavaScript's Core: Is It Built on C or C  ?JavaScript's Core: Is It Built on C or C ?May 05, 2025 am 12:07 AM

JavaScriptisnotbuiltonCorC ;it'saninterpretedlanguagethatrunsonenginesoftenwritteninC .1)JavaScriptwasdesignedasalightweight,interpretedlanguageforwebbrowsers.2)EnginesevolvedfromsimpleinterpreterstoJITcompilers,typicallyinC ,improvingperformance.

JavaScript Applications: From Front-End to Back-EndJavaScript Applications: From Front-End to Back-EndMay 04, 2025 am 12:12 AM

JavaScript can be used for front-end and back-end development. The front-end enhances the user experience through DOM operations, and the back-end handles server tasks through Node.js. 1. Front-end example: Change the content of the web page text. 2. Backend example: Create a Node.js server.

Python vs. JavaScript: Which Language Should You Learn?Python vs. JavaScript: Which Language Should You Learn?May 03, 2025 am 12:10 AM

Choosing Python or JavaScript should be based on career development, learning curve and ecosystem: 1) Career development: Python is suitable for data science and back-end development, while JavaScript is suitable for front-end and full-stack development. 2) Learning curve: Python syntax is concise and suitable for beginners; JavaScript syntax is flexible. 3) Ecosystem: Python has rich scientific computing libraries, and JavaScript has a powerful front-end framework.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use