8-angualr.foreach

爲obj集合中的每個項調用 iterator 函數,它能夠是對象或數組。iterator 函數使用  iterator (value,key,obj)調用,其中value是對象屬性對應的值或數組索引對應元素的值,key是對象屬性或數組元素索引,而obj是obj自己。數組

  值得注意的是。forEach不會迭代繼承的屬性,由於它會使用hasOwnProperty方法進行篩選。函數

遍歷對象:this

var person = {name: 'misko', gender: 'male'};
var log = [];
angular.forEach(person, function(value, key) {
  this.push(key + ': ' + value);
}, log);
---------------------------
log=["name:misko","gender:male"]

遍歷數組:spa

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

  參數以下:code

    objs:須要遍歷的集合對象

    data:遍歷時當前的數據blog

    index:遍歷時當前索引繼承

    array:須要遍歷的集合,每次遍歷時都會把objs原樣的傳一次。索引

    也能夠不用寫後面兩個參數:  it

var objs =[{a:1},{a:2}];
angular.forEach(objs, function(data){
console.log(data.a);
});
本站公眾號
   歡迎關注本站公眾號,獲取更多信息