当前位置:网站首页>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()
}
}
}
边栏推荐
- [operation] MySQL query operation 2 on May 25, 2022
- Sql语句遇到的错误,求解
- Use ideal to connect to the database. The results show some warnings. How to deal with this part
- Thingsboard tutorial (II and III): calculating the temperature difference between two devices in a regular chain
- DO280私有仓库持久存储与章节实验
- Unity 在编辑器中输入字符串时,转义字符的输入
- el-upload上傳文件(手動上傳,自動上傳,上傳進度)
- Interface test tool postman
- Huawei cloud native - data development and datafactory
- Concatenation of Languages(UVA10887)
猜你喜欢
Wang Shuang - assembly language learning summary
(03).NET MAUI实战 基础控件
[cloud native] AI cloud development platform - Introduction to AI model foundry (developers can experience AI training model for free)
Cloud native -- websocket of Web real-time communication technology
[image fusion] multi focus and multi spectral image fusion based on cross bilateral filter and weighted average with matlab code
An error occurs when sqlyog imports the database. Please help solve it!
第十天 数据的保存与加载
NER中BiLSTM-CRF解读score_sentence
Smart use of bitmap to achieve 100 million level massive data statistics
GIS related data
随机推荐
AI落地的新范式,就“藏”在下一场软件基础设施的重大升级里
基于ROS的SLAM建图、自动导航、避障(冰达机器人)
Wang Shuang - assembly language learning summary
2021-11-04
Knowledge - how to build rapport in sales with 3 simple skills
华为云原生——数据开发与DataFactory
如何利用FME 创建自己的功能软件
JS static method
Day 10 data saving and loading
. Net 7 JWT configuration is too convenient!
Interpretation score of bilstm-crf in NER_ sentence
Unity 在編輯器中輸入字符串時,轉義字符的輸入
You know AI, database and computer system
Educoder group purchase suspension box page production
绿色新动力,算力“零”负担——JASMINER X4系列火爆热销中
(03). Net Maui actual combat basic control
《机器人SLAM导航核心技术与实战》第1季:第0章_SLAM发展综述
Technology sharing | broadcast function design in integrated dispatching
网络层详解
[summary of skimming questions] database questions are summarized by knowledge points (continuous update / simple and medium questions have been completed)