forEach method in Arrays – Functional Programming in Javascript Part 3
In Javascript arrays are objects. It has a method forEach(fn) that takes function as argument and executes it for each element of the array. This function can have element of the array, index and array itself as argument.
var myArray=[10, 20, "Hello", {}];
var myFunction=function(item, index, array){
console.log("For an element: "+item);
}
myArray.forEach(myFunction);
Function expression myFunction will be executed for each element of the myArray array.
Jkoder.com Tutorials, Tips and interview questions for Java, J2EE, Android, Spring, Hibernate, Javascript and other languages for software developers



