赞
踩
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <meta charset="utf-8">
- <title>js_arr.forEach()</title>
- </head>
- <body>
- <script>
- /*参考:https://www.runoob.com/jsref/jsref-foreach.html
- 知识点:
- 1.forEach()
- 遍历数组的每个元素,并将元素传递给回调函数。
- 注意: forEach() 对空数组是不会执行回调函数的。
- array.forEach(function(currentValue, index, arr), thisValue)
- function(currentValue, index, arr) 必需。 数组中每个元素需要调用的函数。
- currentValue 必需。当前元素
- index 可选。当前元素的索引值。
- arr 可选。当前元素所属的数组对象。
- thisValue 可选。传递给函数的值一般用 "this" 值。
- 如果这个参数为空, "undefined" 会传递给 "this" 值。
- forEach()方法没有返回值。*/
- let numbers = [4, 9, 16];
- let sum = 0;
- let a = numbers.forEach(function (value, index) {
- /*功能:遍历数组中的每一个元素。*/
- console.log(index, value);
- // 0 4
- // 1 9
- // 2 16
- sum += value;
- });
- console.log("sum:", sum);
- // sum: 29
- console.log("a:", a);
- // a: undefined
- </script>
- </body>
- </html>
-
-
-
-

Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。