angular.foreach or foreach in angularjs

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

Share:   fb twitter linkedin
Kailash...
Participant
24 Points
5 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

Rahul Kiwitech
great work
  
banner

Blog

Active User (2)

 Log In to Chat