当前位置:网站首页>JS traversal array (using the foreach () method)

JS traversal array (using the foreach () method)

2022-06-23 15:11:00 Liujiayi_

forEach() Method You need a function as an argument
- Functions like this , Created by us but not called by us , We call it a callback function
- If there are several elements in the array, the function will execute several times , Every time it is executed , The browser will traverse the elements
Passed in as an argument , We can define formal parameters , Read this
- The browser will be in the callback function Pass three parameters :


The first parameter , Is the element currently being traversed
The second parameter , Is the index of the element currently being traversed

The third parameter , Is the array being traversed

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      var arr = [" The Monkey King ", " Pig eight quit ", " The sand monk ", " Tang's monk ", " Bones jing "];
      arr.forEach(function (value, index, obj) {
        console.log(value);
      });
    </script>
  </body>
</html>


 

原网站

版权声明
本文为[Liujiayi_]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/174/202206231424014276.html