2.2 map

2.2.1 语法:

_.map(list, iteratee, [context])

2.2.2 说明:

对集合的每个成员依次进行某种操作,将返回的值依次存入一个新的数组。接收3个参数。list可理解为数据源iteratee迭代器可理解为回调方法;context执行上下文。

  • list可以操作数组,对象,字符串和arguments
  • iteratee 会传第三个参数(element, index, list)或(value, key, list)
  • iteratee里面需要返回值。
  • context可以改变iteratee内部的this

2.2.3 代码示例:

示例一:map对数组、对象、字符串和arguments进行操作并返回数组。

var result;

//操作数组
result = _.map([1, 2, 3], function (element, index, list) {
return element + 1;
});
console.log(result) //=> [2, 3, 4] //操作对象
result = _.map({one: '一', two: '二', three: '三'}, function(value, key, list){
return value + 1;
});
console.log(result) //=> ["一1", "二1", "三1"] //操作字符串
result = _.map('123', function(element, index, list){
return element + 1;
});
console.log(result) //=> ["11", "21", "31"] //操作arguments
function abc(){
result = _.map(arguments, function(element, index, list){
return element + 1;
});
console.log(result); //=> [2, 3, 4]
}
abc(1, 2, 3);

示例二:iteratee传递的参数

var result;

//数组的情况
result = _.map([1, 2, 3], function (element, index, list) {
console.log(element, index, list);
//=> 1 0 [1, 2, 3]
//=> 2 1 [1, 2, 3]
//=> 3 2 [1, 2, 3]
}); //对象的情况
result = _.map({one: '一', two: '二', three: '三'}, function(value, key, list){
console.log(value, key, list);
//=> 一 one Object {one: "一", two: "二", three: "三"}
//=> 二 two Object {one: "一", two: "二", three: "三"}
//=> 三 three Object {one: "一", two: "二", three: "三"}
});

示例三:iteratee内部需要有return值

var arr1 = _.map([1, 2, 3], function (element, index, list) {
element + 1;
}); var arr2 = _.map([1, 2, 3], function (element, index, list) {
return element + 1;
});
console.log(arr1); //=> [undefined, undefined, undefined]
console.log(arr2); //=> [2, 3, 4]

示例四:context可以改变iteratee内部的this

var result = _.map([1, 2, 3], function (element, index, list) {
return element + this.no; //this为{no : 10}
}, {no : 10}); console.log(result);//=> [11, 12, 13]

示例五:map方法执行后,list不变,返回新数组。

var list = [1, 2, 3];

var result = _.map(list,  function(element, index, list){
return element + 1;
}); console.log(list); //=> [1, 2, 3]
console.log(result); //=> [2, 3, 4]

2.1.4 _.collect的功能和_.map是一样的

var result = _.collect([1, 2, 3],  function(element, index, list){
return element + 1;
});
console.log(result); //=> [2, 3, 4]

2.1.5 操作非集合,返回空数据

var arr1 = _.map(null, function (element, index, list) {
console.log(element); //不执行
}); var arr2 = _.map(undefined, function (element, index, list) {
console.log(element); //不执行
}); var arr3 = _.map(123, function (element, index, list) {
console.log(element); //不执行
}); var arr4 = _.map(new Date(), function (element, index, list) {
console.log(element); //不执行
});
console.log(arr1); //=> []
console.log(arr2); //=> []
console.log(arr3); //=> []
console.log(arr4); //=> []

2.1.6 iteratee还可以是全局的方法

var result = _.map([1, -2, -3], Math.abs);
console.log(result); //=> [1, 2, 3]

2.1.7 iteratee里面用console.log需要bind(坑)

var result = _.map([1, -2, -3], console.log.bind(console));
//=> 1 0 [1, -2, -3]
//=> -2 1 [1, -2, -3]
//=> -3 2 [1, -2, -3]

我在gitbook地址:https://www.gitbook.com/book/niec-fe/underscorejs/details

underscorejs-map学习的更多相关文章

  1. STL的pair学习, map学习

    http://blog.csdn.net/calvin_zcx/article/details/6072286 http://www.linuxidc.com/Linux/2014-10/107621 ...

  2. 不学就吃亏的underscorejs类库学习示例 ——(集合篇)

    underscorejs是一个很不错的类库,我的很多项目都引用了这个类库,的确可以带来很多方便. 记得我当初学的时候,看underscorejs的api是看的一知半解的,甚至不明白api里的conte ...

  3. Java数据结构之Map学习总结

    前言: 前面学习总结了List的使用及效率对比,今天总结学习一下键值映射关系Map,顺便学习一下Android中使用Map需要注意哪些,以及谷歌官方针对Android对Map做了哪些优化. 先了解下M ...

  4. 集合框架之Map学习

    Map接口的实现类有HashTable.HashMap.TreeMap等,文章学习整理了“ Map和HashMap的使用方法”. /** * Map和HashMap的使用方法 */public sta ...

  5. Java_lambda表达式之"stream流学习,Map学习,collect学习,Conllectors工具类学习"

    Lambda表达式学习 对List<Integer> userIdList = UserList.stream().map(User::getUserId).collect(Collect ...

  6. map学习笔记

    collection是单列集合,map是双列集合.其中包含<k,v>键值对,注意:键具有唯一性,而值不唯一. 在此列举三个读取方式:keyset,valueset,及entryset. k ...

  7. Map学习

    1.Query Operations(查询操作) int size();boolean isEmpty(); boolean containsKey(Object key);boolean conta ...

  8. STL之map学习实例

    ``` #include<iostream> #include<algorithm> #include<vector> #include<map> #i ...

  9. Python map学习笔记

    map是一个高阶用法,字面意义是映射,它的作用就是把一个数据结构映射成另外一种数据结构. map用法比较绕,最好是对基础数据结构很熟悉了再使用,比如列表,字典,序列化这些. map的基本语法如下: m ...

  10. golang map学习

    当对map只声明时,由于map为引用类型,所以默认值为nil,但对nil map 而言,支持read ,但不支持write 当执行write操作时, 会抛出panic异常; 代码如下: func Te ...

随机推荐

  1. 洛谷1001 A+B Problem

    洛谷1001 A+B Problem 本题地址:http://www.luogu.org/problem/show?pid=1001 题目描述 输入两个整数a,b,输出它们的和(|a|,|b|< ...

  2. python对拍程序

    import sys; import random; import os; gen=open("data.in","w"); #///生成测试数据 gen.cl ...

  3. lightoj 1063 求割点

    题目链接:http://lightoj.com/volume_showproblem.php?problem=1063 #include<cstdio> #include<cstri ...

  4. python推荐淘宝物美价廉商品 2.0

    改动: 新增功能 :可选择只看天猫或淘宝 代码模块化封装,参数配置或输入单独在一个py文件管理,主函数功能只留出参数传入在setting配置的py文件里. main.py代码: # -*- codin ...

  5. Android中的RelativeLayout

    安卓布局之一,RelativeLayout.又称之为相对布局.对于一个界面每个人都有不同的实现.我比较喜欢使用RelativeLayou.原因是,相对布局不会出现过多的嵌套,在现在硬件不断发展的今天, ...

  6. 批量更新数据小心SQL触发器的陷阱

    批量更新数据时候,Inserted和Deleted临时表也是批量的,但触发器只会调用执行一次!两个概念千万不要弄混淆! 错误的理解:例如:创建在A表上创建了一个Update触发器,里面写的是Updat ...

  7. 账户管理命令 useradd、groupadd

    内容提要: 1. 掌握用户的 增/删/改 命令 2. 掌握组的 增/删/改 命令 组管理 1)groupadd groupadd 用于添加组账号.格式如下: groupadd [-g GID] GRO ...

  8. MySQL更新时Error Code:1093和Error Code:1175的解决办法

    Error Code: 1093. You can't specify target table 'ws_product' for update in FROM clause 这个是我们在使用upda ...

  9. [Firebase] Deploy you website to Firebase

    If you are looking for a host website, you can try Firebase, heroku or AWS... Today, I tried to depl ...

  10. [WebStrom] Change default cmd to Cygwin

    GO to setting, search Terminal: Change shell path : C:\cygwin\bin\bash.exe --login -i    (to the loc ...