angular.forEach()

angular.forEach()

angular.forEach(obj, iterator, [context]);
iterator(value, key, obj)

说明:

Invokes the iterator function once for each item in obj collection, which can be either an object or an array. The iterator function is invoked with iterator(value, key, obj),
where value is the value of an object property or an array element, key is the object property key or array element index and obj is the obj itself. Specifying a context for the function is optional.

理解:

forEach迭代对象或者数组。例如下面代码,value为values中的值,key为values中值对应的key,obj为对象本身。

参数:

Param Type Details
obj Object Array Object to iterate over.
iterator Function Iterator function.
context(optional) Object Object to become context (this) for the iterator function.

返回值:

Object: Reference to obj.
Array

example:

var values = {name: 'misko', gender: 'male'};
var log = [];
var tmpVal = angular.forEach(values, function(value, key, obj) { 
  this.push(key + ': ' + value);
}, log);
console.log(values);
console.log(log);
console.log(tmpVal);

result:

Object {name: "misko", gender: "male"}//values
["name: misko", "gender: male"]//log
Object {name: "misko", gender: "male"}//tmpVal
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容