forEach() 方法用于调用数组的每个元素,并将元素传递给回调函数
forEach() 方法用于调用数组的每个元素,并将元素传递给回调函数
  主要使用场景: 遍历数组的每个元素
注意点:

 

1. forEach 主要是遍历数组
2. 参数当前数组元素是必须要写的, 索引号可选。
    <script>
        // forEach()就是遍历    加强版的for循环    适合用于遍历数组对象
        const arr = ['red', 'green', 'blue']
        const result = arr.forEach(function (item, index) {
            console.log(item);  // 数组元素  red   green  blue
            console.log(index); // 索引号
        })
        //console.log(result);   //不会返回数组
        // 1. forEach 主要是遍历数组
        // 2. 参数当前数组元素是必须要写的, 索引号可选。
    </script>

类似方法还有  map() 和 filter()

后续再整理

 

Logo

为开发者提供学习成长、分享交流、生态实践、资源工具等服务,帮助开发者快速成长。

更多推荐