1. 程式人生 > >Angular.forEach用法

Angular.forEach用法

1.針對物件迴圈(key,value)

官方示例:

複製程式碼
var values = {name: 'misko', gender: 'male'};
var log = [];
angular.forEach(values, function(value, key) {
  this.push(key + ': ' + value);
}, log);
expect(log).toEqual(['name: misko', 'gender: male']);
複製程式碼

2.針對物件陣列(key,value)

非官方示例:

var objs =[{a:1},{a:2}];
angular.forEach(objs, 
function(data,index,array){ //data等價於array[index] console.log(data.a+'='+array[index].a); });

3.針對物件陣列(data)

非官方示例:

var objs =[{a:1},{a:2}];
angular.forEach(objs, function(data){
console.log(data.a);
});

引數:

Param Type Details
obj

Object to iterate over.

iterator

Iterator function.

context (optional)

Object to become context (this) for the iterator function.