Home >Web Front-end >JS Tutorial >How to use the foreach function in JavaScript

How to use the foreach function in JavaScript

不言
不言Original
2018-12-24 16:16:479304browse

In How to use the foreach function in How to use the foreach function in How to use the foreach function in JavaScript, arrays can process multiple data, and the foreach function can perform similar processing on each data in the array. This article will introduce in detail the usage of the foreach function in How to use the foreach function in How to use the foreach function in How to use the foreach function in JavaScript.

How to use the foreach function in How to use the foreach function in How to use the foreach function in JavaScript

Let’s first take a look at the basic syntax of the foreach function

The callback function is a process that is executed on each data of the array.

数组.foreach(回调函数)

We will use the foreach function for iterative processing below

The specific code is as follows

<!DOCTYPE html>
<html>
  <head>
    <meta charset = "utf-8">
    <title>How to use the foreach function in How to use the foreach function in How to use the foreach function in JavaScript</title>
  </head>
  <body>
    <script>
    var cats = [ &#39;波斯猫&#39;, &#39;橘猫&#39;, &#39;蓝猫&#39;, &#39;缅甸猫&#39; ];
    cats.forEach(function( cat ) {
      console.log( cat );
    });
    var runners = [ &#39;张三&#39;, &#39;李四&#39;, &#39;王二&#39;, &#39;陈五&#39; ];
    runners.forEach(function( runner, index ) {
      console.log("第" + (index + 1) + "是" + runner + "同学。" );
    });
    </script>
  </body>
</html>

The running results are as follows

How to use the foreach function in How to use the foreach function in How to use the foreach function in JavaScript

Let's analyze the above code in detail

Code 1:

var cats = [ &#39;波斯猫&#39;, &#39;橘猫&#39;, &#39;蓝猫&#39;, &#39;缅甸猫&#39; ];
cats.forEach(function( cat ) {
console.log( cat );
});

Set an array of "cats" and execute it on each array data foreach processing. The function receives each data in the array as a variable called cat and outputs it in console.log. Repeat this for the data in the array.

As a result, the following string is output.

How to use the foreach function in How to use the foreach function in How to use the foreach function in JavaScript

Code 2:

var runners = [ &#39;张三&#39;, &#39;李四&#39;, &#39;王二&#39;, &#39;陈五&#39; ];
runners.forEach(function( runner, index ) {
console.log( (index + 1) + "第二位是" + runner + "同学。" );
});

The callback function can get the data index number as a parameter.

Set an array of "runners" and perform foreach processing on each array data.

As a result, the following string is output.

How to use the foreach function in JavaScript

This article ends here. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to use the foreach function in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn