当前位置:网站首页>Iterators and generators
Iterators and generators
2022-07-27 08:18:00 【huijie_ 0716】
iterator
One 、for in
Two 、for of
// Use of Traversal string
letstr="helloworld";
for(letsofstr){
console.log(s)
}
// Use of Traversal array
letarr=["hello","world"]
for(letkofarr.keys()){
console.log(k);
}
for(letvofarr.values()){
console.log(v);
}
for(let[k,v]ofarr.entries()){
console.log(k,v);
}
// Use in Traversing objects
letobj={"name":"qiku","addr":" East Third Street "}
for(letkinobj){
console.log(k,obj[k]);
}3、 ... and 、 iterator
1、 iterator Must be able to for of iteration
2、 Iteratable object Is not necessarily iterator
Four 、 Iterative data structures
generator
One 、 Define generator
among * Used to indicate that the function is Generator function ,yield To define a functionInternal state
generator It must be iterator
function * myGen(){
yield 0;
yield 1;
yield 2;
}
var x=myGen();
for(let i of x){
console.log(i);
}
console.log(x.next());
console.log(x.next());
console.log(x.next());
console.log(x.next());
console.log(x);1、 First call next When the method is used , from Generator The head of the function begins to executeThat's ok , First it's printed one, Execute to yield Just stop , And will yield Back42/76 Value of expression '1', As a return object value Property value , At this time, the function has notHave you finished , Return object's done The property value is false.2、 Second call next When the method is used , Same as the previous step .3、 Third call next When the method is used , First it's printed three, Then the function is executedReturn operation , And will return The value of the following expression , As a return objectvalue Property value , At this point, the function has ended , More than done The property value istrue.4、 The fourth call next When the method is used , At this point, the function has been executed , So backvalue The property value is undefined,done The property value is true. If you executeIn the third step , No, return In words , Go straight back {value:undefined,done:true}.
Two 、 Using the generator by object Provide Iterative interface
var obj={
name:' Zhang San ',
age:13,
sex:' male ',
hobby:' football , Volleyball , Basketball '
}
function * objectGen(obj){
for(let i in obj){
yield [i,obj[i]];
}
}
for(let i of objectGen(obj)){
console.log(i);
}
3、 ... and 、 Generated by big data An optimization method
Generate data on demand
function* bigData() {
for (let i = 0; i < 100000; i++) {
yield parseInt(Math.random() * 100000));
}
}
var x = bigData();
var arr = [];
for(let i of x){
arr.push(i);
}
//console.log(arr)Four 、 Using the generator Realization Asynchronous program execution
function* sing() {
for (let i = 0; i < 10; i++) {
yield ' Sing a song ' + i;
}
}
function* dance() {
for (let i = 0; i < 10; i++) {
yield ' dance ' + i;
}
}
function* rap(num) {
for (let i = 0; i < num; i++) {
yield 'rap' + i;
}
}
var s = sing();
var d = dance();
var r = rap(20);
while (true) {
let t1 = s.next();
console.log(t1.value);
let t2 = d.next();
console.log(t2.value);
let t3 = r.next();
console.log(t3.value);
if (t1.done && t2.done && t3.done) {
console.log(' complete ');
break;
}
}边栏推荐
- Solution to the program design of the sequence structure of one book (Chapter 1)
- Plato farm is expected to further expand its ecosystem through elephant swap
- Things come to conform, the future is not welcome, at that time is not miscellaneous, neither love
- [pytorch] resnet18, resnet20, resnet34, resnet50 network structure and Implementation
- 借生态力量,openGauss突破性能瓶颈
- STM32小bug汇总
- Development of three database general SQL code based on PG Oracle and MySQL
- Use of string type "PHP Basics"
- Methods of server network testing
- [target detection] yolov6 theoretical interpretation + practical test visdrone data set
猜你喜欢

On Valentine's day, I drew an object with characters!

All in one 1251 - Fairy Island for medicine (breadth first search)

Design and development of GUI programming for fixed-point one click query

vCenter7.0管理Esxi7.0主机
![[ciscn2019 southeast China division]web11 1](/img/94/61ad4f6cbbd46ff66f361462983d7a.png)
[ciscn2019 southeast China division]web11 1

C event usage case subscription event+=

面试官:什么是脚手架?为什么需要脚手架?常用的脚手架有哪些?

SSTI template injection

Is redis really slowing down?

Download and usage of sequel Pro
随机推荐
我用字符画出了一个谷爱凌!
Modification case of Ruixin micro rk3399-i2c4 mounting EEPROM
开怀一笑
[applet] how to get wechat applet code upload key?
Debug: generic related "unresolved external symbols"
[NPUCTF2020]ReadlezPHP 1
1024 | in the fourth year officially called Menon, the original intention is still there, and continue to move forward
Opengauss stopped from the library and found that the main library could not write data
Binglog backup data
How to log in multiple wechat on the computer
On Valentine's day, I drew an object with characters!
Use of "PHP Basics" Boolean
How to obtain the cash flow data of advertising services to help analyze the advertising effect?
Sword finger offer 58 - I. flip word order
[ten thousand words long article] thoroughly understand load balancing, and have a technical interview with Alibaba Daniel
Use of elastic box / expansion box (Flex)
Risk control and application of informatization project
ERP production operation control Huaxia
Prevent cookies from modifying ID to cheat login
Interviewer: what is scaffolding? Why do you need scaffolding? What are the commonly used scaffolds?