angular.foreach or foreach in angularjs

Views: 2259
Comments: 1
Like/Unlike: 2
Posted On: 24-Dec-2017 01:47 

Share:   fb twitter linkedin
Kailash...
206 Points
23 Posts
Introduction
 
Angularjs provides angular.forEach function that invokes the iterator function that iterates or loops through each item in an array or object.
 
Syntax
 
angular.forEach(object, iterator, [context])
 
  • object: The object interate over.
  • iterator: The iterator function or the code.
  • context: Object to become context (using this) for the iterator function. It is optional
 
angular.forEach(array, function (value, index) {
   //
});
 
Example
 
var employees = [];
var employee = {
  Id: '0012',
  Name : 'Devit',
  Age : 30
};

employees.push(employee);

employee = {
  Id: '0013',
  Name : 'Priti',
  Age : 24
};
employees.push(employee);

angular.forEach(employees, function (employee, i) {

  console.log('employee object');
  //Through value
  console.log(employee);
  //Through index
  console.log(employees[i]);

  console.log('employee Name');
  //Through value
  console.log(employee.Name);
  //Through index
  console.log(employees[i].Name);

});
 
Output
 

 
1 Comments
great work

Rahul Kiwitech
22-Jan-2018 at 23:58
 Log In to Chat