Table of Contents
What is the difference between traversal and iteration in es6
Home Web Front-end Front-end Q&A What is the difference between traversal and iteration in es6

What is the difference between traversal and iteration in es6

Apr 26, 2022 pm 02:57 PM
es6

The difference between traversal and iteration in es6 is: traversal emphasizes taking out the entire data in sequence, accessing all elements of the data structure; while iteration also takes out data in sequence, it does not guarantee how much it will take. , there is no guarantee that all the data will be fetched, it is a form of traversal.

What is the difference between traversal and iteration in es6

The operating environment of this tutorial: Windows 10 system, ECMAScript version 6.0, Dell G3 computer.

What is the difference between traversal and iteration in es6

What is iteration

Continuously retrieve data from a data set in a certain order The process

encapsulates the iterative process, usually an object. Different languages ​​show different forms of iteration

What is traversal

Enables the members of the data structure to be arranged in a certain order;

ES6 creates a new traversal command for...of loop, and the Iterator interface is mainly used for for...of consumption (the object traversed by for of must have a traverser Interface can only be traversed)

Difference

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

Iteration emphasizes that fetching data in sequence does not guarantee how much data will be retrieved, nor does it guarantee that all the data will be fetched

Convenience emphasizes that the entire data must be fetched in sequence

Examples are as follows:

Traverse a common array:

//遍历一个普通数组
const arr = [1,2,3,4,5];
for(let i = 0;i< arr.length;i++){
console.log(arr[i])
}
Copy after login

Iterate a common array:

//迭代一个数组
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)   //这个对象就为迭代器
//附加的功能
//让迭代器自己取数据,知道没有数据为止
let data = iterator.next();
while(!data.done){
console.log(data.value)
data = iterator.next();
}
console.log("迭代完成")
//
Copy after login

[Related recommendations: javascript video tutorialweb front-end

The above is the detailed content of What is the difference between traversal and iteration in es6. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
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

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Is async for es6 or es7? Is async for es6 or es7? Jan 29, 2023 pm 05:36 PM

Is async for es6 or es7?

How to reverse an array in ES6 How to reverse an array in ES6 Oct 26, 2022 pm 06:19 PM

How to reverse an array in ES6

How to find different items in two arrays in es6 How to find different items in two arrays in es6 Nov 01, 2022 pm 06:07 PM

How to find different items in two arrays in es6

Why does the mini program need to convert es6 to es5? Why does the mini program need to convert es6 to es5? Nov 21, 2022 pm 06:15 PM

Why does the mini program need to convert es6 to es5?

Is require an es6 syntax? Is require an es6 syntax? Oct 21, 2022 pm 04:09 PM

Is require an es6 syntax?

What does es6 temporary Zenless Zone Zero mean? What does es6 temporary Zenless Zone Zero mean? Jan 03, 2023 pm 03:56 PM

What does es6 temporary Zenless Zone Zero mean?

Is es6 map ordered? Is es6 map ordered? Nov 03, 2022 pm 07:05 PM

Is es6 map ordered?

Is es2015 the same as es6? Is es2015 the same as es6? Oct 25, 2022 pm 06:51 PM

Is es2015 the same as es6?

See all articles