Produces a new array of values by mapping each value in list through a transformation function (iterator). If the native map method exists, it will be used instead. If list is a JavaScript object, iterator's arguments will be (value, key, list).

所有的javascript对象元素都将经过回调函数作用

1 _.map([1, 2, 3], function(num){ return num * 3; });
2 => [3, 6, 9]
3 _.map({one : 1, two : 2, three : 3}, function(num, key){ return num * 3; });
4 => [3, 6, 9]

源码:

 1  _.map = _.collect = function(obj, iterator, context) {
 2     var results = [];
 3     if (obj == null) return results;
 4     if (nativeMap && obj.map === nativeMap) return obj.map(iterator, context);
 5     each(obj, function(value, index, list) {
 6       results[results.length] = iterator.call(context, value, index, list);
 7     });
 8     if (obj.length === +obj.length) results.length = obj.length;
 9     return results;
10   };

 

 

 

posted on 2012-04-20 23:12  himanhimao  阅读(518)  评论(0编辑  收藏  举报