Home  >  Article  >  Web Front-end  >  How to output the value of an array in jquery?

How to output the value of an array in jquery?

青灯夜游
青灯夜游Original
2020-11-23 15:56:293574browse

The "$.each()" method can be used in jquery to loop through the array elements, and then output the array elements one by one in the loop body; the syntax "$.each(arr,function(index,value){ console.log(index ":" value);})".

How to output the value of an array in jquery?

Related recommendations: "jQuery Video Tutorial"

The operating environment of this tutorial: windows7 system, jquery version 2.2.4 , this method is suitable for all brands of computers.

In jquery, you can use the $.each() method to loop through array elements to output all array elements.

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>JQuery循环遍历输出数组元素示例</title>
    <script src="https://cdn.staticfile.org/jquery/2.2.4/jquery.min.js"></script>
</head>
<body>
	<script>
		$(document).ready(function(){
            var arr=["Apple", "Banana", "Orange", "Mango", "Pineapple"];
            $.each(arr, function(index, value){
                console.log(index + ": " + value + &#39;<br>&#39;);
            });
        });
    </script>
</body>
</html>

$.each() method has two parameters. The first parameter is the name of the array to be looped, and the second parameter is a method.

Here we define an arr array, and then pass an array index (index) and corresponding array value (value) to each callback in this method, and then loop out the specified array through the append() method The result of the value is printed.

The results are as follows:

How to output the value of an array in jquery?

For more programming-related knowledge, please visit: Programming Video Course! !

The above is the detailed content of How to output the value of an array in jquery?. 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