作用:通过转换函数(iteratee迭代器)映射列表中的每个值产生价值的新数组。iteratee传递三个参数:value,然后是迭代 index。

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

调用过程:

1.

  // Return the results of applying the iteratee to each element.
_.map = _.collect = function(obj, iteratee, context) {
iteratee = cb(iteratee, context);
var keys = !isArrayLike(obj) && _.keys(obj),
length = (keys || obj).length,
results = Array(length);
for (var index = 0; index < length; index++) {
var currentKey = keys ? keys[index] : index;
results[index] = iteratee(obj[currentKey], currentKey, obj);
}
return results;
};

2. cb函数  应该是callback的缩写。

  // An internal function to generate callbacks that can be applied to each
// element in a collection, returning the desired result — either `identity`,
// an arbitrary callback, a property matcher, or a property accessor.
var cb = function(value, context, argCount) {
if (_.iteratee !== builtinIteratee) return _.iteratee(value, context);
if (value == null) return _.identity;
if (_.isFunction(value)) return optimizeCb(value, context, argCount);
if (_.isObject(value)) return _.matcher(value);
return _.property(value);
};

这里等于又接着调用optimizeCb,关于optimizeCb在_.each分析中有介绍,不在复述。

underscore.js 分析6 map函数的更多相关文章

  1. underscore.js中的节流函数debounce及trottle

    函数节流   throttle and debounce的相关总结及想法 一开始函数节流的使用场景是:放止一个按钮多次点击多次触发一个功能函数,所以做了一个clearTimeout setTimeou ...

  2. jQuery 源码分析(五) map函数 $.map和$.fn.map函数 详解

    $.map() 函数用于使用指定函数处理数组中的每个元素(或对象的每个属性),并将处理结果封装为新的数组返回,该函数有三个参数,如下: elems Array/Object类型 指定的需要处理的数组或 ...

  3. js parseInt和map函数

    今天看了一个js的题目["1","2","3"].map(parseInt),看到后脑海中浮现的答案是[1,2,3],但是看到正确答案后蒙了 ...

  4. underscore.js 分析 第三天

    // Create a safe reference to the Underscore object for use below. // 为Underscore对象创建一个安全的引用 // _为一个 ...

  5. underscore.js 分析 第一天

    Underscore 是一个非常实用的Javascript类库. 通过研究他能提高自身的JS水平. 我们看到整个代码被 (function() { /*  代码 */ }.call(this)); 包 ...

  6. js foreach、map函数

    语法:forEach和map都支持2个参数:一个是回调函数(item,index,input)和上下文: •forEach:用来遍历数组中的每一项:这个方法执行是没有返回值的,对原来数组也没有影响: ...

  7. Underscore.js (1.7.0)-函数预览

    集合(Collections)(25) - each - map - reduce - reduceRight - find - filter - where - findWhere - reject ...

  8. underscore.js 分析 第四天

    查看underscore包含多少属性和方法 通过阅读JavaScript 获取对象的键的数组 var a = _; var arr = Object.keys(a); console.log(arr) ...

  9. underscore.js 分析 第二天

    Underscore源码中有这么句obj.length === +obj.length意思是typeof obj.length == number,即检测obj的长度是否是数字我的理解:这么写是来检测 ...

随机推荐

  1. 关于sys.dm_exec_requests

    我知道SQL Server有很多视图和函数让我来了解SQL Server的运行状态.我还想知道SQL Server上关于来自用户或者应用的活动请求信息.怎么查询这些信息呢? SQL Server的动态 ...

  2. 关于sys CPU usage 100%问题的分析

    最近一个客户抱怨他的核心EBS数据库出现性能问题.这是一个10.2.0.3的数据库,运行在Red Hat Enterprise Linux Server release 5.5 (Linux x86- ...

  3. REST Framework组件的解析源码

    首先我们要知道解析器的作用 解析器就是对你请求体中的数据进行反序列化.封装 把你的所有的请求数据都封装在request.data中 以后就在request.data中获取数据 我们先导入rest_fr ...

  4. js实现svg图形转存为图片下载[转]

    我们知道canvas画布可以很方便的js原生支持转为图片格式并下载,但是svg矢量图形则并没有这方面原生的支持.研究过HighChart的svg图形的图片下载机制,其实现原理大体是浏览器端收集SVG代 ...

  5. Java基础加强之集合

    集合整体框架图 各集合框架的概述 1. Collection(常用List和Set,不常用Queue和Vector),单元素集合. 2. Map(常用HashMap和TreeMap,不常用HashTa ...

  6. springmvc IDEA

    回顾Java平台上Web开发历程来看,从Servlet出现开始,到JSP繁盛一时,然后是Servlet+JSP时代,最后演化为现在Web开发框架盛行的时代.一般接触到一个新的Web框架,都会想问这个框 ...

  7. php官网下载的chm手册,源码字号太小的问题解决

    首先,到官方网站上下载chm格式的文档,地址如下: http://php.net/downloads.php 如图,点击荧光笔标出链接 然后就可以看到各种语言版本的文档手册,可以选择中文版,并带有笔记 ...

  8. leetcode566. Reshape the Matrix

    https://leetcode.com/problems/reshape-the-matrix/description/ public int[][] matrixReshape(int[][] n ...

  9. 2018年秋季学期《C语言程序设计I》教学过程及学期总结

    一学期下来,问题很多,思考也很多,需要整理.总结,好的经验要形成规律,不好的地方要提示警醒. 教学过程小结: C语言程序设计I-第一周教学 C语言程序设计I-第三周教学 C语言程序设计I-第四周教学 ...

  10. mysql数据库使用insert语句插入中文数据报错

    在mysql的命令行模式中,通过insert语句插入中文数据的时候报错,类似于下面这样: Incorrect string value: '\xE7\x8F' for column 'name' at ...