Iterator和for...of循环→for...of循环

阳光穿透心脏的1/2处 2022-10-02 12:54 378阅读 0赞
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  7. <title>for...of循环</title>
  8. </head>
  9. <body>
  10. <script>
  11. const fruits = ['apple', 'banana', 'orange', 'mango'];
  12. /*
  13. ES5
  14. */
  15. for (let i = 0; i < fruits.length; i++) {
  16. console.log(fruits[i])
  17. }
  18. fruits.forEach(friut => {//forEach缺点不能中止和跳过
  19. // if(friuts[0]==='apple'){//报错:Uncaught ReferenceError: friuts is not defined at fruits.forEach.friut (for...of循环.html:23) at Array.forEach (<anonymous>) at for...of循环.html:22
  20. // console.log(friuts[0])
  21. // }
  22. console.log(friut);
  23. })
  24. for (let friut in fruits) {//这里的friut获取的是下标
  25. console.log(friut);//0 1 2 3
  26. }
  27. /*
  28. for...in循环遍历对象上可枚举的所有属性
  29. */
  30. fruits.describe = 'my favorite friuts';
  31. for (let index in fruits) {
  32. console.log(fruits[index]);//apple banana orange mango my favorite friuts
  33. }
  34. /*
  35. ES6 for...of循环
  36. 支持中止和跳过
  37. */
  38. for (let friut of fruits) {
  39. console.log(friut);//apple banana orange mango
  40. }
  41. for (let friut of fruits) {
  42. if (friut === 'orange') {//支持中止
  43. break;
  44. }
  45. console.log(friut);//apple banana
  46. }
  47. for (let friut of fruits) {
  48. if (friut === 'orange') {//支持跳过
  49. continue;
  50. }
  51. console.log(friut);//apple banana mango
  52. }
  53. </script>
  54. </body>
  55. </html>
  56. <!DOCTYPE html>
  57. <html lang="en">
  58. <head>
  59. <meta charset="UTF-8">
  60. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  61. <meta http-equiv="X-UA-Compatible" content="ie=edge">
  62. <title>for...of实例</title>
  63. <style>
  64. .completed{
  65. text-decoration: line-through;
  66. }
  67. </style>
  68. </head>
  69. <body>
  70. <ul>
  71. <li>X</li>
  72. <li>Y</li>
  73. <li>Z</li>
  74. </ul>
  75. <script>
  76. const fruits = ['apple', 'banana', 'orange', 'mango'];
  77. /*
  78. entries() 方法返回一个新的Array Iterator对象,该对象包含数组中每个索引的键/值对。
  79. console.log(fruit)结果为:
  80. (2) [0, "apple"]
  81. (2) [1, "banana"]
  82. (2) [2, "orange"]
  83. (2) [3, "mango"]
  84. 注解:0,1,2,3是索引
  85. */
  86. for (let fruit of fruits.entries()) {
  87. console.log(fruit);
  88. }
  89. /*
  90. 注意:我现在想通过entries()方法,
  91. 直接获得值('apple', 'banana', 'orange', 'mango')
  92. */
  93. for (let [index, fruit] of fruits.entries()) {
  94. console.log(fruit);
  95. }
  96. //将下标值(index)和值结合起来,注意下标值是从0开始的;
  97. for (let [index, fruit] of fruits.entries()) {
  98. console.log(`第${index + 1}天,吃${fruit}`);
  99. }
  100. /*
  101. sum(4,30,623,1447,8,501,475,6)里面的数字加起来,操作如下;
  102. arguments 是一个对应于传递给函数的参数的类数组对象。
  103. arguments对象是所有(非箭头)函数中都可用的局部变量。
  104. */
  105. function sum() {
  106. // console.log(arguments);
  107. let total = 0;
  108. for (let num of arguments) {
  109. total += num;
  110. }
  111. console.log(total);
  112. return total;
  113. }
  114. sum(4, 30, 623, 1447, 8, 501, 475, 6)//3094
  115. /*
  116. for...of循环利用与字符串
  117. */
  118. let name = 'xyz';
  119. for (let char of name) {
  120. console.log(char);//x y z
  121. }
  122. /*
  123. for...of循环小操作
  124. querySelectorAll:
  125. 返回与指定的选择器组匹配的文档中的元素列表 (使用深度优先的先序遍历文档的节点)。
  126. 返回的对象是 NodeList 。
  127. */
  128. const lis=document.querySelectorAll('li');
  129. console.log(lis);//NodeList(3) [li, li, li]
  130. for(let li of lis){
  131. li.addEventListener('click',function(){
  132. this.classList.toggle('completed');
  133. })
  134. }
  135. </script>
  136. </body>
  137. </html>

发表评论

表情:
评论列表 (有 0 条评论,378人围观)

还没有评论,来说两句吧...

相关阅读

    相关 iterator详解以及for循环的区别

    迭代器是一种模式,它可以使得对于序列类型的数据结构的遍历行为与被遍历的对象分离,即我们无需关心该序列的底层结构是什么样子的。只要拿到这个对象,使用迭代器就可以遍历这个对象...