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>

 

posted @ 2014-04-12 22:17  草珊瑚  阅读(331)  评论(0编辑  收藏  举报