foreach in jquery code example

Example 1: jquery loop through array

var arr = ['one','two','three','four','five'];
$.each(arr, function(index, value){
	console.log('The value at arr[' + index + '] is: ' + value);
});

Example 2: jquery each

$( "li" ).each(function( index ) {
  console.log( index + ": " + $( this ).text() );
});

Example 3: jquery loop over elements

$( "li" ).each(function( index ) {
  console.log( index + ": " + $( this ).text() );
});

Example 4: javascript foreach

const avengers = ['thor', 'captain america', 'hulk'];
avengers.forEach((item, index)=>{
	console.log(index, item)
})

Example 5: jquery each

//Array
$.each( arr, function( index, value ){
    sum += value;
});

//Object
$.each( obj, function( key, value ) {
    sum += value;
});

Example 6: jquery foreach

$.each( obj, function( key, value ) {
  alert( key + ": " + value );
});

Tags:

Php Example