AngularJS開發學習筆記: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 ObjectArray

Object to iterate over.

iterator Function

Iterator function.

context

(optional)

Object

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

 

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章