js_arr.forEach().html

<!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>




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值