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 ...
随机推荐
- hdu 4419 Colourful Rectangle
http://acm.hdu.edu.cn/showproblem.php?pid=4419 题意:给出3种颜色,重叠会生成新的颜色,然后有一些矩形,求出每种颜色的面积. 转化为二进制表示颜色:001 ...
- NSIS脚本根据操作系统版本动态决定默认安装目录
问题描述: 因为windows XP和windows 7的program files不同(有program files(x86)),所以需要动态根据系统的位数设置默认安装目录 References: ...
- 图片延迟加载scrollLoading.js应用
<ul> <li><a href="http://news.qq.com/" target="_b ...
- 利用内存结构及多线程优化多图片下载(IOS篇)
利用内存结构及多线程优化多图片下载(IOS篇) 前言 下载地址, 后续发布, 请继续关注本blog 在IOS中,我们常常遇到多图片下载的问题.最简单的解决方案是直接利用别人写好的框架.但是这如同练武, ...
- 如何在程序中调用Caffe做图像分类
Caffe是目前深度学习比较优秀好用的一个开源库,采样c++和CUDA实现,具有速度快,模型定义方便等优点.学习了几天过后,发现也有一个不方便的地方,就是在我的程序中调用Caffe做图像分类没有直接的 ...
- poj 3111 K Best (二分搜索之最大化平均值之01分数规划)
Description Demy has n jewels. Each of her jewels has some value vi and weight wi. Since her husband ...
- hdu 5433 Xiao Ming climbing(bfs+三维标记)
Problem Description Due to the curse made by the devil,Xiao Ming is stranded on a mountain and can ...
- Promise 异步执行的同步操作
Promise 是用来执行异步操作的. 但有时一个异步操作需要等其他的异步操作完成,这时候就可以使用then来做. function loadImageAsync(url) { return new ...
- 在Struts2中使用Uploadify组件上传文件
Uploadify是一个基于Jquery的文件上传组件,官网http://www.uploadify.com/可以在官网获得该组件,运行演示示例,下载帮助文档. 作为Web前端的增强技术,Jq ...
- (3)选择元素——(6)属性选择器(Attribute selectors)
Attribute selectors are a particularly helpful subset of CSS selectors. They allow us to specify an ...