Can jquery traverse array array?

青灯夜游
Release: 2021-11-16 14:06:38
Original
2334 people have browsed it

jquery can traverse the array array. Methods: 1. Use the for loop statement to traverse the array; 2. Use the "for in" loop statement to traverse the array; 3. Use the forEach() method to traverse the array; 4. Use each( ) method to traverse the array; 5. Use the map() method to traverse the array.

Can jquery traverse array array?

The operating environment of this tutorial: windows7 system, jquery1.10.2 version, Dell G3 computer.

jquery traverses array array

1. for loop:

var arr = new Array(13.5,3,4,5,6);
for(var i=0;i<arr.length><p>2. for in loop: </p><pre class="brush:js;toolbar:false;">var x
var mycars = new Array()
mycars[0] = "Saab"
mycars[1] = "Volvo"
mycars[2] = "BMW"
for (x in mycars)
{
  document.write(mycars[x] + "<br />")
}
Copy after login

3. forEach Method traversal

<script>
	var arr = [1,3,5,7,9];
	var obj = {0:1,1:3,2:5,3:7,4:9};
	/**
	*利用原生JS的forEach方法遍历
	*第一个参数:遍历到的元素
	*第二个参数:当前遍历到的索引
	*注意:原生的forEach方法只能遍历数组,而不能遍历伪数组
	*/
	arr.forEach(function(value,index){
		console.log("原生forEach遍历数组:",index,value);
	});
	/**
	obj.forEach(function(value,index){
		console.log("原生forEach遍历伪数组:",index,value);
		//Uncaught TypeError: obj.forEach is not a function
	});
	*/

</script>
Copy after login

Can jquery traverse array array?

4, each() traverses the array

<script>
	var arr = [1,3,5,7,9];
	var obj = {0:1,1:3,2:5,3:7,4:9};

	/**
	* 利用jQuery的each静态方法遍历
	* 第一个参数:当前遍历到的索引
	* 第二个元素:遍历到的元素
	* 注意:jQuery的each方法可以遍历伪数组
	*/
	$.each(arr,function(index,value){
		console.log("jQuery-each方法遍历数组:",index,value);
	})
	$.each(obj,function(index,value){
		console.log("jQuery-each方法遍历伪数组:",index,value);
	})
</script>
Copy after login

Can jquery traverse array array?

##5, map() traverses Array

<script>
	var arr = [1,3,5,7,9];
	var obj = {0:1,1:3,2:5,3:7,4:9};
	/**
	*1.利用原生JS的map方法遍历 
	*第一个参数:遍历到的元素
	*第二个参数:当前遍历到的索引
	*第三个参数:当前被遍历的数组
	*注意:和原生的forEach方法一样,不能遍历伪数组
	*/
	arr.map(function(value,index,array){
		console.log("原生map遍历数组:",index,value,array);
	});
	/**
	obj.map(function(value,index,array){
		console.log("原生map遍历伪数组:",index,value,array);
		//Uncaught TypeError: obj.forEach is not a function
	});
	*/

	/**
	* 2.利用jQuery的each静态方法遍历
	* 第一个参数:要遍历的数组
	* 每遍历一个元素之后执行的回调函数
	* 回调函数的参数:
	* 	第一个参数:遍历到的元素
	* 	第二个元素:当前遍历到的索引
	* 注意:和jQuery的each方法一样可以遍历伪数组
	*/
	$.map(arr,function(value,index){
		console.log("jQuery-map方法遍历数组:",index,value);
	})
	$.map(obj,function(value,index){
		console.log("jQuery-map方法遍历伪数组:",index,value);
	})
</script>
Copy after login

Can jquery traverse array array?

Recommended related tutorials:

jQuery video tutorial

The above is the detailed content of Can jquery traverse array array?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!