当前位置:   article > 正文

js_arr.forEach().html_foreach 返回html

foreach 返回html
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="utf-8">
  5. <title>js_arr.forEach()</title>
  6. </head>
  7. <body>
  8. <script>
  9. /*参考:https://www.runoob.com/jsref/jsref-foreach.html
  10. 知识点:
  11. 1.forEach()
  12. 遍历数组的每个元素,并将元素传递给回调函数。
  13. 注意: forEach() 对空数组是不会执行回调函数的。
  14. array.forEach(function(currentValue, index, arr), thisValue)
  15. function(currentValue, index, arr) 必需。 数组中每个元素需要调用的函数。
  16. currentValue 必需。当前元素
  17. index 可选。当前元素的索引值。
  18. arr 可选。当前元素所属的数组对象。
  19. thisValue 可选。传递给函数的值一般用 "this" 值。
  20. 如果这个参数为空, "undefined" 会传递给 "this" 值。
  21. forEach()方法没有返回值。*/
  22. let numbers = [4, 9, 16];
  23. let sum = 0;
  24. let a = numbers.forEach(function (value, index) {
  25. /*功能:遍历数组中的每一个元素。*/
  26. console.log(index, value);
  27. // 0 4
  28. // 1 9
  29. // 2 16
  30. sum += value;
  31. });
  32. console.log("sum:", sum);
  33. // sum: 29
  34. console.log("a:", a);
  35. // a: undefined
  36. </script>
  37. </body>
  38. </html>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/正经夜光杯/article/detail/1005401
推荐阅读