<!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的更多相关文章

  1. guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用

    guava 学习笔记(二) 瓜娃(guava)的API快速熟悉使用 1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection ...

  2. [转]深入理解Java 8 Lambda(类库篇——Streams API,Collectors和并行)

    以下内容转自: 作者:Lucida 微博:@peng_gong 豆瓣:@figure9 原文链接:http://zh.lucida.me/blog/java-8-lambdas-insideout-l ...

  3. 瓜娃《guava》api快速入门

    1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection API Guava Basic Utilities IO API C ...

  4. Lambda类库篇 —— Streams API, Collector和并行

    本文是深入理解Java 8 Lambda系列的第二篇,主要介绍Java 8针对新增语言特性而新增的类库(例如Streams API.Collectors和并行). 本文是对 Brian Goetz 的 ...

  5. guava 学习笔记 瓜娃(guava)的API快速熟悉使用

    1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection API Guava Basic Utilities IO API C ...

  6. 学习笔记(二) 瓜娃(guava)的API快速熟悉使用

    1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection API Guava Basic Utilities IO API C ...

  7. 深入理解Java 8 Lambda(类库篇——Streams API,Collectors和并行)

    转载:http://zh.lucida.me/blog/java-8-lambdas-inside-out-library-features/ 关于 深入理解 Java 8 Lambda(语言篇——l ...

  8. guava API整理

    1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: Introduction Guava Collection API Guava Basic Utilities IO API C ...

  9. 【转】瓜娃(guava)的API快速熟悉使用

    http://www.cnblogs.com/snidget/archive/2013/02/05/2893344.html 1,大纲 让我们来熟悉瓜娃,并体验下它的一些API,分成如下几个部分: I ...

随机推荐

  1. 暴力破解UltraEdit v21 无需注册

    一.复制一份UltraEdit安装目录中的主程序uedit32.exe,到任意目录,用UltraEdit打开复制的uedit32.exe文件. 二.修改以下内容 原来:00094750h: BE DC ...

  2. C51函数的递归调用

    前几天在写C51程序时用到了递归,简单程序如下: void WRITE_ADD(uchar addr,uchar wbyte) { START(); //先发送起始信号 WRITE_BYTE(0xa0 ...

  3. 从头到尾彻底理解KMP(2014年8月22日版)

    http://blog.csdn.net/v_july_v/article/details/7041827

  4. 一篇非常经典的springMVC注解实现方式详解

    今天公司让搭建个springMVC的注解框架,研究了好半天,网络搜罗了半天,好不容易找到篇,拿来分享下: 原文出处:http://itxxz.com/a/kuangjia/2014/0531/4.ht ...

  5. 【CF 549G Happy Line】排序

    题目链接:http://codeforces.com/problemset/problem/549/G 题意:给定一个n个元素的整数序列a[], 任意时刻对于任一对相邻元素a[i-1]. a[i],若 ...

  6. poj2739

                                                                                           Sum of Consec ...

  7. apacheOfbiz

    一.ofbiz 用自身数据库安装 1. 由 binary 安装: 由 binary 安装非常简单, 以下是安装方法: 下载ofbiz-2.0-beta1-complete.tar.gz, 注意不是of ...

  8. Linux内存点滴 用户进程内存空间

    Linux内存点滴 用户进程内存空间 经常使用top命令了解进程信息,其中包括内存方面的信息.命令top帮助文档是这么解释各个字段的. VIRT, Virtual Image (kb) RES, Re ...

  9. Jquery_Ajax文件上传

    如何实现jQuery的Ajax文件上传,PHP如实文件上传.AJAX上传文件,PHP上传文件. [PHP文件上传] 在开始之前,我觉得是有必要把通WEB上传文件的原理简单说一下的.实际上,在这里不管是 ...

  10. python爬爬(网友提供学习)

    import urllib2,urllib,os,re def ZZ(url): pathw=os.getcwd() #图片和标题目录 imagetitleregion=r'<div class ...