underscoreJS的Collections 的API
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>集合Collections</title>
<script type="text/javascript" src="underscore.js"></script>
<script type="text/javascript">
// each函数,对数组里的每个元素,进行处理,返回原来数组
console.log("each函数");
_.each([1, 2, 3], function(value, key){
console.log(value);
console.log(key);
}); _.each({one: 1, two: 2, three: 3},function(value, key){
console.log(value);
console.log(key);
}); // map函数,对数组里的每个元素,进行处理,返回处理过的数组
console.log("map函数");
var new_arr = _.map([1, 2, 3], function(num){ return num * 3; });
console.log(new_arr);
var new_arr = _.map({one: 1, two: 2, three: 3}, function(num, key){ return num * 3; });
console.log(new_arr); // reduce函数,第三个参数是memo值,返回的是处理过的memo值
console.log("reduce函数");
var sum = _.reduce([1, 2, 3], function(memo, num){ return memo + num; }, 1);
console.log(sum); // reduceRight函数,功能同reduce函数
var list = [[0, 1], [2, 3], [4, 5]];
var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
console.log("reduceRight函数");
console.log(flat); // even函数,找到匹配的元素,函数将立即返回,不会遍历
var even = _.find([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
console.log("find函数");
console.log(even); // filter函数,找到匹配的元素,会遍历
var evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
console.log("filter函数");
console.log(evens); // where函数,找到匹配的对象,包括key和value。找不到返回[]
var listOfPlays = [{title: "Cymbeline", author: "Shakespeare", year: 1611},
{title: "The Tempest", author: "Shakespeare", year: 1611},
{title: "The Tempest", author: "Shakespeare", year2: 1611},
{title: "The Tempest", author2: "Shakespeare", year: 1611},
{title: "The Tempest", author: "not", year: 1611},
{title: "The Tempest", author: "Shakespeare", year: 1612}
];
var new_list = _.where(listOfPlays, {author: "Shakespeare", year: 1611});
var new_list2 = _.where(listOfPlays, {author: "Shakespeare333", year: 1611});
console.log("where函数");
console.log(new_list);
console.log(new_list2); // findWhere函数,找到第一个匹配的对象,包括key和value,马上返回。找不到返回undefined
var listOfPlays = [{title: "Cymbeline", author: "Shakespeare", year: 1611},
{title: "The Tempest", author: "Shakespeare", year: 1611},
{title: "The Tempest", author: "Shakespeare", year2: 1611},
{title: "The Tempest", author2: "Shakespeare", year: 1611},
{title: "The Tempest", author: "not", year: 1611},
{title: "The Tempest", author: "Shakespeare", year: 1612}
];
var new_list = _.findWhere(listOfPlays, {author: "Shakespeare", year: 1611});
var new_list2 = _.findWhere(listOfPlays, {author: "Shakespeare222", year: 1611});
console.log("findWhere函数");
console.log(new_list);
console.log(new_list2); // reject函数,和filter函数相反
var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
console.log("reject函数")
console.log(odds); // every函数,如果每个函数都正确匹配,返回true,否则返回false
var evens = _.every([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
console.log("every函数");
console.log(evens);
var evens = _.every([2, 4, 6], function(num){ return num % 2 == 0; });
console.log(evens); // some函数,只要有一个匹配正确,就返回true
var evens = _.some([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
console.log("some函数");
console.log(evens); // contains函数,也是只要有一个匹配正确,就返回true
var containFlag = _.contains([1, 2, 3], 3);
console.log("contains函数");
console.log(containFlag); // invoke函数,每个元素调用指定方法。和map方法不同之处在于,map是当作参数,而invoke是当做函数调用主体
var new_arr = _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
console.log("invoke函数");
console.log(new_arr); // pluck函数,返回对象数组里,指定key的value
var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
var new_arr = _.pluck(stooges, 'name');
console.log("pluck函数");
console.log(new_arr); // max函数,筛选出拥有某个最大key指定的value,的对象
var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
var max = _.max(stooges, function(stooge){ return stooge.age; });
console.log("max函数");
console.log(max);
console.log(_.max([1,2,3,4])); // min函数,作用和max函数相反
console.log("min函数");
console.log(_.min([1,2,3,4])); // sortBy,排序
console.log("sortBy函数");
console.log(_.sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); })); // groupBy函数,分组
console.log("groupBy函数");
console.log(
_.groupBy([1.3, 2.1, 2.4], function(num){ return Math.floor(num); })
);
console.log(
_.groupBy(['one', 'two', 'three'], 'length')
); // indexBy函数,主要用于数组对象,作用和groupBy类似。如果key不是唯一,后面的key会覆盖前一个key。
var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}, {"name":'sam',age:50}];
console.log(
_.indexBy(stooges, 'age')
); // countBy,有条件的统计
console.log("countBy函数");
console.log(
_.countBy([1, 2, 3, 4, 5], function(num) {
return num % 2 == 0 ? 'even': 'odd';
})
); // shuffle 乱序
console.log("shuffle函数");
console.log(
_.shuffle([1, 2, 3, 4, 5, 6])
); // sample,返回一个指定的随机范围
console.log("sample函数");
console.log(
_.sample([1,2,3,4,5,6],3)
); // size, 返回list长度
console.log("size函数");
console.log(
_.size({one: 1, two: 2, three: 3})
); // partition,将一个数组拆分成两个数组,按条件
console.log("partition函数");
console.log(
_.partition([0, 1, 2, 3, 4, 5], function(value){
return (value>2);
})
); </script>
</head>
<body> </body>
</html>
underscoreJS的Collections 的API的更多相关文章
- guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用
guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用 1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection ...
- [转]深入理解Java 8 Lambda(类库篇——Streams API,Collectors和并行)
以下内容转自: 作者:Lucida 微博:@peng_gong 豆瓣:@figure9 原文链接:http://zh.lucida.me/blog/java-8-lambdas-insideout-l ...
- 瓜娃《guava》api快速入门
1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection API Guava Basic Utilities IO API C ...
- Lambda类库篇 —— Streams API, Collector和并行
本文是深入理解Java 8 Lambda系列的第二篇,主要介绍Java 8针对新增语言特性而新增的类库(例如Streams API.Collectors和并行). 本文是对 Brian Goetz 的 ...
- guava 学习笔记 瓜娃(guava)的API快速熟悉使用
1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection API Guava Basic Utilities IO API C ...
- 学习笔记(二) 瓜娃(guava)的API快速熟悉使用
1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection API Guava Basic Utilities IO API C ...
- 深入理解Java 8 Lambda(类库篇——Streams API,Collectors和并行)
转载:http://zh.lucida.me/blog/java-8-lambdas-inside-out-library-features/ 关于 深入理解 Java 8 Lambda(语言篇——l ...
- guava API整理
1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection API Guava Basic Utilities IO API C ...
- 【转】瓜娃(guava)的API快速熟悉使用
http://www.cnblogs.com/snidget/archive/2013/02/05/2893344.html 1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: I ...
随机推荐
- 使用AlarmManager进行定时任务处理
1:UploadingService.java package com.example.service; import com.example.broadcast.AlarmReceiver; imp ...
- 管理Activity 用户在主界面按两次回退退出系统
1:定义一个用于管理Activity的类. /* * 用于管理Activity */ public class SysApp extends Application{ private List< ...
- POJ 1379 Run Away
题意:有n个陷阱,在X,Y范围内要求出一个点使得这个点到陷阱的最小距离最大. 思路:模拟退火,随机撒入40个点,然后模拟退火随机化移动. (这题poj坑爹,加了srand(time(NULL))不能交 ...
- Windows 8.1 with Update 镜像下载(增OEM单语言版)
该系统已有更新的版本,请转至<Windows 8.1 with update 官方最新镜像汇总>下载. 2014年4月9日凌晨,微软向MSDN订阅用户开放了Windows 8.1 with ...
- Qt读取ANSI格式文件——利用QTextCodec将其他编码格式转换为Unicode格式
Qt使用Unicode来表示字符串.但是通常需要访问一些非Unicode格式的字符串,例如打开一个GBK编码的中文文本文件,甚至一些非Unicode编码的日文,俄文等. Qt提供了QTextCodec ...
- Android和FTP服务器交互,上传下载文件(实例demo)
今天同学说他备份了联系人的数据放在一个文件里,想把它存到服务器上,以便之后可以进行下载恢复..于是帮他写了个上传,下载文件的demo 主要是 跟FTP服务器打交道-因为这个东东有免费的可以身亲哈 1. ...
- UML--核心元素之包
包是一种容器,如同文件夹一样. 包是UML非常常用的一个元素,它最主要的作用就是容纳并为其他元素分类.包可以容纳用例.业务实体.类图等,也包含子包. 分包的原则 1.如果将元素分为三个包A.B.C,那 ...
- Eclipse自动补全功能轻松设置 || 不需要修改编辑任何文件
本文介绍如何设置Eclipse代码自动补全功能.轻松实现输入任意字母均可出现代码补全提示框. Eclipse代码自动补全功能默认只包括 点"." ,即只有输入”." ...
- UVa1399.Ancient Cipher
题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...
- 飘逸的python - 性能调优利器profile及其意义
VIM 的作者Bram Moolenaar在一篇叫高效文本编辑器的7个习惯的ppt中有这么一段话. Three basic steps 1. Detect inefficiency 2. ...