each function javascript 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 loop over elements

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

Example 3: javascript foreach

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

Example 4: javascript foreach

var colors = ['red', 'blue', 'green'];

colors.forEach(function(color) {
  console.log(color);
});

Example 5: for each js

const fruits = ['mango', 'papaya', 'pineapple', 'apple'];

// Iterate over fruits below

// Normal way
fruits.forEach(function(fruit){
  console.log('I want to eat a ' + fruit)
});

Example 6: for each javascript

let numbers = ['one', 'two', 'three', 'four'];

numbers.forEach((num) => {
  console.log(num);
});  // one   //two //three // four

Tags:

Php Example