In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article will explain in detail what Iterator is in js. The editor thinks it is very practical, so I share it with you as a reference. I hope you can get something after reading this article.
What is Iterator and what is its function?
Iterator is a prerequisite for understanding Article 61. Maybe I didn't have enough IQ for Iterator and Generator to read it many times or had little knowledge. Even if I understood it at that time, I forgot all about it after a while.
Iterator (iterator) is an interface, or a specification. Provide a unified access mechanism for different data structures. Any data structure can be traversed as long as the Iterator interface is deployed (that is, all members of the data structure are processed in turn).
Iterator syntax:
Const obj = {[Symbol.iterator]: function () {}}
The [Symbol.iterator] attribute name is written in a fixed way, and as long as an object with this property is owned, it can be traversed as an iterator.
The iterator's traversal method is to first get a pointer to the iterator, which initially points to the first piece of data, and then changes the pointer's point by calling the next method, so that each time the next points to the next piece of data, it returns an object that has two properties.
Value represents the data you want to get.
Done Boolean value. False indicates that the data pointed to by the current pointer has a value, and true indicates that the traversal is over.
There are three functions of Iterator:
Provide a unified and simple access interface for various data structures.
Enables the members of the data structure to be arranged in a certain order
ES6 created a new traversal command, for. Of loop, Iterator interface is mainly for for. Of consumption.
Traverse the process:
Creates a pointer object that points to the starting position of the current data structure. In other words, the traversal object is essentially a pointer object.
The first time you call the next method of the pointer object, you can point the pointer to the first member of the data structure.
The second time you call the next method of the pointer object, the pointer points to the second member of the data structure.
The next method of the pointer object is called until it points to the end of the data structure.
Each time the next method is called, information about the current member of the data structure is returned. Specifically, it returns an object that contains value and done properties. Where the value property is the value of the current member and the done property is a Boolean value indicating whether the traversal ends.
Let arr = [{num:1}, 2Power3] let it = arr [Symbol.iterator] () / / get the iterator console.log (it.next ()) / {value: Object {num:1}, done: false} console.log (it.next ()) / / {value: 2, done: false} console.log (it.next ()) / / {value: 3 Done: false} console.log (it.next ()) / / {value: undefined, done: true} this is the end of the article on what Iterator is in js. Hope that the above content can be helpful to you, so that you can learn more knowledge, if you think the article is good, please share it for more people to see.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.