underscore.js小记录(集合、数组篇)

underscore.js集合、数组相关函数的一些联系与区别…

集合(Collection Functions)

  1. .find()和.filter()的联系和区别

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    _.find(list, predicate, [context]) 别名: detect
    _.filter(list, predicate, [context]) 别名: select
    _.reject(list, predicate, [context])
    说明:_.find()返回第一个通过predicate真值检测的值,_.filter()则返回全部,
    而_.reject()则返回所有未通过predicate真值检测的值。
    eg:
    _.find([1,4,5], function(item){
    return item > 1;
    });
    // 4
    _.filter([1,4,5], function(item){
    return item > 1;
    });
    // [4, 5]
    _.filter([1,4,5], function(item){
    return item > 4;
    });
    // [5]
    _.reject([1,4,5], function(item){
    return item > 4;
    });
    // [1, 4]
  2. .findWhere()和.where()的联系和区别

    1
    2
    3
    4
    5
    6
    7
    8
    说明:
    _.findWhere(list, properties): 筛选出第一个含有properties的项
    _.where(list, properties): 筛选出所有含有properties的项
    eg:
    _.findWhere([{a:1,b:2},{b:2,c:3},{b:3,d:4}],{b:2});
    // {a: 1, b: 2}
    _.where([{a:1,b:2},{b:2,c:3},{b:3,d:4}],{b:2});
    // [{a: 1, b: 2}, {b:2, c:3}]
  3. .invoke()实例、.pluck()实例

    1
    2
    3
    4
    5
    6
    说明:_.pluck()用于萃取若干对象的某个属性值
    eg:
    _.invoke([[1,2,0],[-1,1,0]],"toString");
    // ["1,2,0", "-1,1,0"]
    _.pluck([{name:"lucy",age:22}, {name:"Bob",age:23}, {name:"John",age:34}], ["name"]);
    // ["lucy", "Bob", "John"]
  4. _.max()实例

    1
    2
    3
    4
    5
    6
    7
    说明:_.max()不仅可以用于获取数组中的最大值,也可以用于获取某属性值取得最大值的对象
    eg:
    var arr = [{a:1,b:3},{a:3,b:2},{a:3,b:4}];
    _.max(arr, function(item){
    return item.a;
    });
    // {a: 3, b: 2}
  5. .sortBy()、.groupBy()、.indexBy()、.countBy()的联系与区别

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    _.sortBy(list, iteratee, [context]): 排序
    _.groupBy(list, iteratee, [context]):分组
    _.indexBy(list, iteratee, [context]):分组(键值唯一)
    _.countBy(list, iteratee, [context]):分组计数
    eg:
    _.sortBy([{name:"Bob",age:"34"},{name:"Alice",age:"23"},{name:"Lucy",age:"26"}],"age");
    // [{name: "Alice", age: "23"},{name: "Lucy", age: "26"},{name: "Bob", age: "34"}]
    _.groupBy([{a:1,b:3},{a:3,b:4},{a:2,b:2}], function(item){
    return item.b > 2;
    });
    // {false: [{a: 2, b: 2}], true: [{a: 1, b: 3},{a: 3, b: 4}]}
    _.indexBy([{a:1,b:3,id:"001"},{a:3,b:4,id:"002"},{a:2,b:2,id:"003"}], function(item){
    return item.id;
    });
    // {001: {a: 1, b: 3, id: "001"}, 002: {a: 3, b: 4, id: "002"}, 003: {a: 2, b: 2, id: "003"}}
    _.countBy([{a:1,b:3},{a:3,b:4},{a:2,b:2}], function(item){
    return item.b > 2;
    });
    // {true: 2, false: 1}

数组(Array Functions)

  1. .first()、.last()、.initial()、.rest()实例

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    说明:
    _.first(n): 前几个
    _.last(n): 后几个
    _.initial(n): 除了后几个剩下的
    _.rest(n): 除了前几个剩下的
    eg:
    _.first([1,2,3,4,5],2);
    // [1, 2]
    _.last([1,2,3,4,5],2);
    // [4, 5]
    _.initial([1,2,3,4,5],2);
    // [1, 2, 3]
    _.rest([1,2,3,4,5],2);
    // [3, 4, 5]
  2. .union()、.intersection()

    1
    2
    3
    4
    5
    6
    7
    8
    说明:
    _.union(*arrays): 数组并集
    _.intersection(*arrays): 数组交集
    eg:
    _.union([1,3],[3,4]);
    // [1, 3, 4]
    _.intersection([1,2],[2,3]);
    // [2]