当前位置:网站首页>Iterator of JS
Iterator of JS
2022-06-30 04:10:00 【Runqing】
Application of iterators
Data processing : Summarize the numbers in the following objects
let a = {
b: {
c: [
'1',
'2',
'3'
],
d: [
'4',
'5',
'6',
'7'
],
e: [
'8',
'9',
'10'
]
}
}
// Not available here for...of, Is due to Object Of prototype Not on [Symbol.iterator] Method , Therefore, complains
for (let o of a) {
console.log(o)
}
// Uncaught TypeError: authors is not iterable
If you want to traverse the object , First, the data format can be changed
for (let key in a) {
let r = []
for (let k in a[key]) {
r = r.concat(a[key][k])
}
console.log(r)
}
// ["1", "2", "3", "4", "5", "6", "7", "8", "9", "10"]
Second, through customization Iterator Interface , Make it traversable
a[Symbol.iterator] = function () {
let b = this.b
let keys = Reflect.ownKeys(b)
let values = []
return {
next () {
if (!values.length) {
if (keys.length) {
values = b[keys[0]]
keys.shift()
}
}
return {
done: !values.length,
value: values.shift()
}
}
}
}
// Defined [Symbol.iterator] After the interface , Can be for...of Iterative
for (let o of a) {
console.log(o)
}
Iterative protocol
If you make an object traversable , We must abide by the iterative protocol , Requirements of the agreement
1. Object to deploy a Symbol.iterator by key Key
2. Symbol.iterator The value of is a parameterless function that complies with the iterator protocol
iterator protocol
The iterator protocol requires the following conditions :
1. An object must contain a parameterless function next
2.next The return value is also an object , contain done and value attribute . among done Indicates whether the traversal ends ,value Returns the value of the current traversal .
With default Iterator The data type of the interface
ES6 Some data structures of Iterator Interface ( For example, array ), I.e. no handling , Can be for…of Loop traversal . The reason lies in , These data structures are deployed natively Symbol.iterator attribute :
Array
Map
Set
String
TypedArray
Functional arguments object
NodeList object
Generator and Iterator
Generator Naturally, it satisfies the iterative protocol , utilize Generator There is no need to show the write iteration protocol ( because Generator contain next Methods and contains done、value Property ).
// use generator Rewrite the iterator interface function above :
a[Symbol.iterator] = function * () {
let b = this.b
let keys = Reflect.ownKeys(b)
let values = []
while (1) {
if (!values.length) {
if (keys.length) {
values = b[keys[0]]
keys.shift()
yield values.shift()
} else {
return false
}
} else {
yield values.shift()
}
}
}
边栏推荐
猜你喜欢

Solve the problem of Navicat connecting to the database

【论文阅读|深读】Role2Vec:Role-Based Graph Embeddings

解决navicat连接数据库遇到的问题

Share an example of a simple MapReduce method using a virtual machine

【模糊神经网络预测】基于模糊神经网络实现水质预测含Matlab源码

两个月拿到N个offer,什么难搞的面试官在我这里都不算事

技术分享| 融合调度中的广播功能设计

dotnet-exec 0.5.0 released

(Reprinted) an article will take you to understand the reproducing kernel Hilbert space (RKHS) and various spaces

如何通过进程启动来分析和解决EasyCVR内核端口报错问题?
随机推荐
(03).NET MAUI实战 基础控件
UML diagrams and list collections
2021-11-04
Ananagrams(UVA156)
2021-07-14
如何通过进程启动来分析和解决EasyCVR内核端口报错问题?
UML图与List集合
接口测试--如何分析一个接口?
Titanic(POJ2361)
Unity when entering a string in the editor, escape the input of characters
SQL append field
技术分享| 融合调度中的广播功能设计
mysql更新数组形式的json串
解决navicat连接数据库遇到的问题
You know AI, database and computer system
A minimalist way to integrate databinding into activity/fragment
487-3279(POJ1002)
【论文阅读|深读】Role2Vec:Role-Based Graph Embeddings
Interface test tool postman
SQLyog导入数据库时报错,求帮解决!