Angular.forEach用法

来源:互联网 发布:原生js请求接口 编辑:程序博客网 时间:2024/06/05 09:00

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);});

 

参数:

ParamTypeDetailsobjObjectArray

Object to iterate over.

iteratorFunction

Iterator function.

context
(optional)
Object

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