1. Loop through an Array

Javascript:
var arr =[1,2,4,5,6];

$.each(arr,function(index,obj)
{
	//We can access the object using obj or this
	alert("index:"+index + " , value: "+obj +" , value:"+this);
});
Output:


2. Loop through JSON Array

Javascript:
var arr2 =[{
	name:"Ravi",
	age:33,
	loc:"India",
},
{
	name:"Haya",
	age:4,
	loc:"United States"
},
{
	name:"Geek",
	age:4,
	loc:"Singapore"
}];

$.each(arr2,function(index,obj)
{
	alert("Name: "+obj.name+"\n"+"Age:"+obj.age+"\n"+"Location: "+obj.loc+"\n");
});	
Output:


3. Loop through an Object

Javascript:
var obj = { name: "ravi", age: 33, loc: "India", gender: function() { return "M"} }
$.each(obj,function(prop,obj)
{
	alert("prop:"+prop + " , value: "+obj);

	if($.type(obj) == "function")
	{
		alert(" gender:" + obj());  //we can call the function also
	}
});

Output:


4. Loop through DOM Elements(Links)

HTML:
<a href="http://google.com">Google</a>
<a href="http://facebook.com">Facebook</a>
<a href="http://twitter.com">Twitter</a>
<a href="http://yahoo.com">Yahoo</a>
Javascript:
$('a').each(function(index,obj)
{
	//You can use obj or this
	alert($(obj).attr("href")+"  "+$(this).html());
	
});
Output:


Please Share it with your friends: