迭代语句即for-of循环

for-of:

在 es6 中引入 通常循环有计数器、检查条件、更新计数器。 for-of 循​​环没有这样的东西。 continue-break 两者都可以搭配使用。 旨在为我们提供当前元素。

1

2

3

4

5

6

7

8

9

10

11

12

13

fruits = [banana,apple,peach,orange,mango,guava,water-melon];

for(const item of fruits){

console.log(item);

}

banana

apple

peach

orange

mango

guava

water-melon

登录后复制

1

2

3

4

5

6

7

8

9优质资源网点我wcqh.cn

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

– If an array if looped over in the form of array.entries(), then the result will be each element in form of an array with index : value.

for(const item of fruits.entries()){

console.log(item);

}

[ 0, banana ]

[ 1, apple ]

[ 2, peach ]

[ 3, orange ]

[ 4, mango ]优质资源网点我wcqh.cn

[ 5, guava ]

[ 6, water-melon ]

// Transform it into a single array comprising of sub-arrays:

fruits.entries(); // Object [Array Iterator] {}

[…fruits.entries()];

// [ [ 0, banana ], [ 1, apple ], [ 2, peach ], [ 3, orange ], [ 4, mango ], [ 5, guava ], [ 6, water-melon ] ]

// Transform into a single ar优质资源网点我wcqh.cnray using for-of loop:

-> Method 1

for(const item of fruits.entries()){

console.log(`${item[0] + 1} : ${item[1]}`);

}

// 1 : banana 2 : apple 3 : peach 4 : orange 5 : mango 6 : guava 7 : water-melon

-> Method 2

for(const [i,el] of fruits.entries()){

console.log(`${i + 1} : ${el}`);

}

// 1 : banana 2 : a优质资源网点我wcqh.cnpple 3 : peach 4 : orange 5 : mango 6 : guava 7 : water-melon

登录后复制

以上就是迭代语句即for-of循环的详细内容,更多请关注青狐资源网其它相关文章!

© 版权声明
THE END
喜欢就支持一下吧
点赞915 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容