Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What are the differences between traversal and iteration in es6

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces the relevant knowledge of "what is the difference between traversal and iteration in es6". The editor shows you the operation process through an actual case. The method of operation is simple, fast and practical. I hope this article "what is the difference between traversal and iteration in es6" can help you solve the problem.

The difference between traversal and iteration in es6 is that traversal emphasizes taking out the whole data in turn and accesses all the elements of the data structure; although iteration also fetches data in turn, it does not guarantee how much or all the data is taken out, which is a form of traversal.

This tutorial operating environment: windows10 system, ECMAScript version 6. 0, Dell G3 computer.

The difference between traversal and iteration in es6

What is iteration?

The process of constantly fetching data from a data set in a certain order.

The encapsulation of the iterative process is usually an object, and the iterative form is different in different languages.

What is traversal?

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 (objects traversed by for of must have an ergodic interface to traverse)

Difference

Traversal is to access all the elements of a data structure, and iteration is a form of traversal.

Iteration emphasizes that fetching data in turn does not guarantee how much to go, nor does it guarantee that all the data will be fetched.

Convenience emphasizes the need to take out the whole data in turn.

Examples are as follows:

Traverse a normal array:

/ / traversing an ordinary array const arr = [1, let, 2, 3, 4, and 5]; for (I = 0 and I)

< arr.length;i++){console.log(arr[i])} 迭代一个普通数组: //迭代一个数组const iterator = { //用于迭代数组的对象i : 0,next(){var result = {// value : ?// done : ?value : arr[this.i],done : this.i >

= arr.length} this.i + +; return result;}} console.log (iterator) / / this object is the iterator / / additional function / / Let the iterator fetch the data itself, knowing that let data = iterator.next (); while (! data.done) {console.log (data.value) data = iterator.next () } console.log ("iterative completion") / / this is the end of the content about "what is the difference between traversal and iteration in es6". Thank you for reading. If you want to know more about the industry, you can follow the industry information channel. The editor will update different knowledge points for you every day.

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report