<!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. BZOJ1013 球形空间产生器sphere

    Description 有一个球形空间产生器能够在n维空间中产生一个坚硬的球体.现在,你被困在了这个n维球体中,你只知道球面上n+1个点的坐标,你需要以最快的速度确定这个n维球体的球心坐标,以便于摧毁 ...

  2. MVC4+Springnet+Nhibernate学习系列随笔(一)

    Springnet与asp.net mvc4集成大体步骤 1.首先要在MVC项目中引用的两个程序集(Spring.Web与Spring.Web.Mvc4) 集 2.修改MVC项目的Global.asa ...

  3. UESTC_王之盛宴 2015 UESTC Training for Graph Theory<Problem K>

    K - 王之盛宴 Time Limit: 3000/1000MS (Java/Others)     Memory Limit: 65535/65535KB (Java/Others) Submit  ...

  4. Hdu4742-Pinball Game 3D(cdq分治+树状数组)

    Problem Description RD is a smart boy and excel in pinball game. However, playing common 2D pinball ...

  5. 【HDU3371】Connect the Cities(MST基础题)

    注意输入的数据分别是做什么的就好.还有,以下代码用C++交可以过,而且是500+ms,但是用g++就会TLE,很奇怪. #include <iostream> #include <c ...

  6. UVa232.Crossword Answers

    题目链接:http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem& ...

  7. 《Algorithms 4th Edition》读书笔记——2.4 优先队列(priority queue)-Ⅶ(延伸:堆排序的实现)

    2.4.5 堆排序 我们可以把任意优先队列变成一种排序方法.将所有元素插入一个查找最小元素的有限队列,然后再重复调用删除最小元素的操作来将他们按顺序删去.用无序数组实现的优先队列这么做相当于进行一次插 ...

  8. 04747_Java语言程序设计(一)_第3章_面向对象编程基础

    链式编程 每次调用方法后,返回的是一个对象 /* * 链式编程 * 每次调用方法后,返回的是一个对象 */ class Student { public void study() { System.o ...

  9. runtime的基本应用

    1.什么是runtime? runtime是一套底层的C语言API,包含很多强大实用的C语言数据类型和C语言函数,平时我们编写的OC代码,底层都是基于runtime实现的. 2.runtime有什么作 ...

  10. 修改UITextField placeholder Color

    [YourtextField setValue:[UIColor colorWithRed:97.0/255.0 green:1.0/255.0 blue:17.0/255.0 alpha:1.0] ...