1、_.first():返回array(数组)的第一个元素。传递 n参数将返回数组中从第一个元素开始的n个元素

_.first = _.head = _.take = function(array, n, guard) {
  //n == null-->array[0];
  //n != null;guard == ture;-->array[0]
  //n != null;guard == false;-->返回前n个元素
return (n != null) && !guard ? slice.call(array, 0, n) : array[0];
};

2、_.initial():返回数组中除了最后一个元素外的其他全部元素。 在arguments对象上特别有用。传递 n参数将从结果中排除从最后一个开始的n个元素

_.initial = function(array, n, guard) {
  //n == null--1;
  //n != null;guard == true--1
  //n != null;guard != true--n
return slice.call(array, 0, array.length - ((n == null) || guard ? 1 : n));
};
//倒序指定的n个元素
slice.call(array, Math.max(array.length-n, 0));

3、_.last():返回array(数组)的最后一个元素。传递 n参数将返回数组中从最后一个元素开始的n个元素,跟first相反

_.last = function(array, n, guard) {
if ((n != null) && !guard) {
return slice.call(array, Math.max(array.length - n, 0));
} else {
return array[array.length - 1];
}
};

4、_.rest():返回数组中除了第一个元素外的其他全部元素。传递 index 参数将返回除了第一个元素到第index元素以外剩余的所有元素,会输出index个元素,跟_.initail相反。underscore不同版本,会函数的定义都不同。

_.rest = _.tail = function(array, index, guard) {
//guard=true,index无效;slice()输出包含start
  return slice.call(array, (index == null) || guard ? 1 : index);
};

5、_.compact:返回能被转为true的元素 filter返回满足验证的元素

_.compact = function(array) {
return _.filter(array, function(value){ return !!value; });
};

6、_.flatten:多维数组转为一维数组 reduce对元素进行迭代,迭代器返回memo,shallow参数用于控制合并深度, 当shallow为true时, 只合并第一层, 默认进行深层合并

_.flatten = function(array, shallow) {
return _.reduce(array, function(memo, value) {
//shallow=true,concat value:链接两个或多个数组
if (_.isArray(value)) return memo.concat(shallow ? value : _.flatten(value));
//value为值,数组自增赋值
memo[memo.length] = value;
return memo;
}, []);
};

7、_.without():返回一个删除所有values值的 array副本。与_.difference类似;前者的第二参数为元素,后者的第二参数为数组

_.without = function(array) {
   // slice.call(arguments, 1)返回从第二个开始的所有参数
return _.difference(array, slice.call(arguments, 1));
};

8、_.uniq():返回 array去重后的副本, 使用 === 做相等测试.
如果您确定 array 已经排序, 那么给 isSorted 参数传递 true值, 此函数将运行的更快的算法.

_.uniq = _.unique = function(array, isSorted, iterator) {
//如何判断第二个参数是isSorted还是iterator,先判断iterator是否存在。
//如果iterator存在,则会根据它创建一个新的数组,以它做对比
var initial = iterator ? _.map(array, iterator) : array;
var results = [];
//length=2,则一定有序
if (array.length < 3) isSorted = true;
//把数组元素归为统一的值
_.reduce(initial, function (memo, value, index) {
//isSorted=true:当前元素与最后一个元素比较:如果不相等的话,有可能跟之前的元素相等呀(已经是有序了,所以相等的都是相邻的)
//isSorted=false:使用include来检查
if (isSorted ? _.last(memo) !== value || !memo.length : !_.include(memo, value)) {
//memo存得时iterator的值
memo.push(value);
//results存的是array的值
results.push(array[index]);
}
return memo;
}, []);
return results;
};

9、_.union():与uniq方法作用一致, 不同之处在于union允许在参数中传入多个数组

_.union = function() {
//true:只合并一层
return _.uniq(_.flatten(arguments, true));
};

10、_.intersection():获取多个数组的交集元素;

_.intersection = _.intersect = function(array) {
var rest = slice.call(arguments, 1);
//filter:满足条件的返回
//先对array去重,uniq只会处理第一个array
//rest是剩余的,需要比较的
return _.filter(_.uniq(array), function(item) {
return _.every(rest, function(other) {
return _.indexOf(other, item) >= 0;
});
});
};

11、_.difference():返回当前数组中与指定数据不相等的差异数据,第二个参数为array,

_.difference = function(array) {
  //只合并一层
var rest = _.flatten(slice.call(arguments, 1), true);
return _.filter(array, function(value){ return !_.include(rest, value); });
};

12、_.zip():将每个数组的相同位置的数据作为一个新的二维数组返回, 返回的数组长度以传入参数中最大的数组长度为准, 其它数组的空白位置使用undefined填充,zip方法应该包含多个参数, 且每个参数应该均为数组

_.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
=> [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]] 
_.zip = function() {
var args = slice.call(arguments);
//pluck:获取对象数组的属性值
var length = _.max(_.pluck(args, 'length'));
var results = new Array(length);
//arguments都是数组
for (var i = 0; i < length; i++) results[i] = _.pluck(args, "" + i);
return results;
}; 

13、_.indexof():搜索一个元素在数组中首次出现的位置, 如果元素不存在则返回 -1,搜索时使用 === 对元素进行匹配

_.indexOf = function(array, item, isSorted) {
if (array == null) return -1;
var i, l;
if (isSorted) {
i = _.sortedIndex(array, item);
      //写的真好,以后不用if了
return array[i] === item ? i : -1;
}
if (nativeIndexOf && array.indexOf === nativeIndexOf) return array.indexOf(item);
for (i = 0, l = array.length; i < l; i++) if (i in array && array[i] === item) return i;
return -1;
};

14、_.lastIndexOf():返回一个元素在数组中最后一次出现的位置, 如果元素不存在则返回 -1,搜索时使用 === 对元素进行匹配,与indexof相反

_.lastIndexOf = function(array, item) {
if (array == null) return -1;
if (nativeLastIndexOf && array.lastIndexOf === nativeLastIndexOf) return array.lastIndexOf(item);
var i = array.length;
while (i--) if (i in array && array[i] === item) return i;
return -1;
};

15、_.range():创建数组,内容为连续数字

_.range = function(start, stop, step) {
if (arguments.length <= 1) {
//如果start=undefined,则stop为0;如果start不为undefined,则stop=start
stop = start || 0;
start = 0;
}
step = arguments[2] || 1; var len = Math.max(Math.ceil((stop - start) / step), 0);
var idx = 0;
var range = new Array(len); while(idx < len) {
range[idx++] = start;
start += step;
} return range;
};

  

underscore arrays的更多相关文章

  1. 你可能不再需要Underscore

    过去几年像 Underscore 和 lodash 等库进入许多JavaScript程序员的工具函数中.虽然这些工具库可以使你的代码写起来更容易,但是他们不一定使代码更简单或更容易理解. 各种工具函数 ...

  2. Underscore.js 初探

    一. 简介   Underscore 这个单词的意思是“下划线”.   Underscore.js 是一个 JavaScript 工具库,提供了一整套的辅助方法供你使用.   Think that - ...

  3. underscore 笔记

    //reduce是汇总 递归 var perDesc=[ {name: "haha", "email": "haha@email.com"} ...

  4. Lo-Dash – 替代 Underscore 的优秀 JS 工具库

    前端开发人员大都喜欢 Underscore,它的工具函数很实用,用法简单.这里给大家推荐另外一个功能更全面的 JavaScript 工具——Lo-Dash,帮助你更好的开发网站和 Web 应用程序. ...

  5. underscore.js 一个强大的js函数库

    Underscore提供的100多个函数,主要涉及对Collection.Object.Array.Function的操作: Collections(集合) each, map, reduce, re ...

  6. underscore.js框架使用

    Underscore.js是一个很精干的库,压缩后只有4KB.它提供了几十种函数式编程的方法,弥补了标准库的不足,大大方便了JavaScript的编程.MVC框架Backbone.js就将这个库作为自 ...

  7. underscore api 概览

    underscore 集合函数(数组或对象) _.each(list, iteratee, [context]); _.map(list, iteratee, [context]); _.reduce ...

  8. 认识Underscore

    Underscore一个JavaScript实用库,提供了一整套函数式编程的实用功能,但是没有扩展任何JavaScript内置对象.它弥补了部分jQuery没有实现的功能,同时又是Backbone.j ...

  9. UnderScore.jsAPI记录

    Collection Functions (Arrays or Objects) each         _.each(list, iterator, [context]) 遍历list中的所有元素 ...

随机推荐

  1. EasyUI使用小常识

    datagrid:1 //显示某列 $('#ListTable').datagrid('showColumn', 'ExRate'); //隐藏某列 $('#ListTable').datagrid( ...

  2. WebAPI认证与授权

    Web APi之认证(Authentication)及授权(Authorization)[一](十二) http://www.cnblogs.com/CreateMyself/p/4856133.ht ...

  3. Excel2013复制内容粘贴到刷选的数据表中

    如何将复制内容粘贴到筛选后表格中: 1.需要一个表格:我要把这个表格里姓名列不包含A的项改为另外一个表中的数据列: 2.在姓名右侧插入两列,列A.列B: 3.在列A下第一格输入1,向下序列填充: 4. ...

  4. leetcode232

    public class MyQueue { Stack<int> S = new Stack<int>(); /** Initialize your data structu ...

  5. Android Studio中由于gradle插件版本和gradle版本对应关系导致的编译失败的问题

    今天在Android Studio中导入新项目,import之后编译报错,报错信息基本都是和版本相关,查询gradle版本相关知识,了解到gradle插件版本和gradle版本有相应的匹配关系,对应如 ...

  6. Spring Boot实践——多线程

    多线程 Spring通过任务执行器(TaskExecutor)来实现多线程和并发编程.使用ThreadPoolTaskExecutor可实现一个基于线程池的TaskExecutor.而实际开发中任务一 ...

  7. Spring cloud @RefreshScope使用

    参数 @RestController @RefreshScope public class HomeController { @Value("${foo}") String foo ...

  8. mpg123解码相关

    int attribute_align_arg mpg123_decode(mpg123_handle *mh, const unsigned char *inmemory, size_t inmem ...

  9. 【319】Python 通过 Twilio 发短信

    参考:python利用twilio模块给自己发短信 参考:使用python实现往手机发短信(基于twilio) 步骤如下: 登录 Twilio 网站注册,貌似需要***,包括用户名.密码.手机号.项目 ...

  10. 向ArcGIS的ToolBarControl中添加任意的windows组建的方法[转]

    向ArcGIS的ToolBarControl中添加任意的windows组建的方法[转] Link: http://www.cnblogs.com/mymhj/archive/2012/10/12/27 ...