underscore arrays
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的更多相关文章
- 你可能不再需要Underscore
过去几年像 Underscore 和 lodash 等库进入许多JavaScript程序员的工具函数中.虽然这些工具库可以使你的代码写起来更容易,但是他们不一定使代码更简单或更容易理解. 各种工具函数 ...
- Underscore.js 初探
一. 简介 Underscore 这个单词的意思是“下划线”. Underscore.js 是一个 JavaScript 工具库,提供了一整套的辅助方法供你使用. Think that - ...
- underscore 笔记
//reduce是汇总 递归 var perDesc=[ {name: "haha", "email": "haha@email.com"} ...
- Lo-Dash – 替代 Underscore 的优秀 JS 工具库
前端开发人员大都喜欢 Underscore,它的工具函数很实用,用法简单.这里给大家推荐另外一个功能更全面的 JavaScript 工具——Lo-Dash,帮助你更好的开发网站和 Web 应用程序. ...
- underscore.js 一个强大的js函数库
Underscore提供的100多个函数,主要涉及对Collection.Object.Array.Function的操作: Collections(集合) each, map, reduce, re ...
- underscore.js框架使用
Underscore.js是一个很精干的库,压缩后只有4KB.它提供了几十种函数式编程的方法,弥补了标准库的不足,大大方便了JavaScript的编程.MVC框架Backbone.js就将这个库作为自 ...
- underscore api 概览
underscore 集合函数(数组或对象) _.each(list, iteratee, [context]); _.map(list, iteratee, [context]); _.reduce ...
- 认识Underscore
Underscore一个JavaScript实用库,提供了一整套函数式编程的实用功能,但是没有扩展任何JavaScript内置对象.它弥补了部分jQuery没有实现的功能,同时又是Backbone.j ...
- UnderScore.jsAPI记录
Collection Functions (Arrays or Objects) each _.each(list, iterator, [context]) 遍历list中的所有元素 ...
随机推荐
- Python yield详解***
yield的英文单词意思是生产,有时候感到非常困惑,一直没弄明白yield的用法. 只是粗略的知道yield可以用来为一个函数返回值塞数据,比如下面的例子: def addlist(alist): f ...
- Webpack-simple cross-env 不是内部或外部命令问题处理
本文转载自:https://www.cnblogs.com/stono/p/6984222.html Webpack-simple cross-env 不是内部或外部命令问题处理 学习了:https: ...
- Repeater使用技巧
一.在ItemDataBound事件里面动态改变Repeater控件里面的html元素 如: <asp:Repeater ID="Repeater1" runat=" ...
- Python 中的属性访问与描述符
在Python中,对于一个对象的属性访问,我们一般采用的是点(.)属性运算符进行操作.例如,有一个类实例对象foo,它有一个name属性,那便可以使用foo.name对此属性进行访问.一般而言,点(. ...
- WPF DatePicker 默认显示当前时间
两种方法: 1.通过后台赋值: this.datePicker.SelectedDate = DateTime.Now; 2.前台控件的属性直接赋值 xmlns:sys="clr-names ...
- A configuration error occurred during startup.Please verify the preference filed with the prompt:Connect to VM
1. 检查JDK,及Tomcat是否正确可用.2. Tomcat,myeclipse使用的是不是同一个jdk.3. 检查系统的防火墙是不是阻止了MyEclipse主程序访问网络.
- TMS Grid
TMS Grid http://edn.embarcadero.com/article/42553
- 用MyEclipse JPA创建项目
http://www.myeclipsecn.com/learningcenter/persistence-development/myeclipse-jpa/ 用MyEclipse JPA创建项目 ...
- Spring Data JPA 基本使用
Spring Data 简化开发,支持Nosql和关系型数据库, DEMO https://github.com/easonstudy/boot-demo/tree/master/boot-sprin ...
- Elasticsearch 2.4.1 Bigdesk 插件安装
简介: Elasticsearch 2.4.1 安装 bigdesk bigdesk 是一个 ES 集群监控工具,可以检测到集群状态.各节点信息,包括 JVM.Thread Pools.OS.Proc ...