前端框架——Angular.js程序员

angular.forEach()

2016-04-28  本文已影响417人  在路上_W

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
上一篇下一篇

猜你喜欢

热点阅读