zoukankan      html  css  js  c++  java
  • UnderScore的使用实例记录

    集合操作

    _.range(),主要用于区间的获取操作。参数说明:(param1):范围上限,(param1,param2):起始及结束范围,(param1,param2,param3):起始结束范围,正数则为区间跨度,-1则为:负数区间跨度。

    _.range(10);
    => [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
    _.range(1, 11);
    => [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    _.range(0, 30, 5);
    => [0, 5, 10, 15, 20, 25]
    _.range(0, -10, -1);
    => [0, -1, -2, -3, -4, -5, -6, -7, -8, -9]
    _.range(0);
    => []

    _.each(list,function(c,i){}),主要用于遍历每个元素进行相应处理操作。参数说明:list为具体遍历的数组,function(c,i)为具体的遍历情况处理,c为具体遍历实体参数,i为具体的index

    _.each([1, 2, 3], alert);
    => alerts each number in turn...
    _.each({one: 1, two: 2, three: 3}, alert);
    => alerts each number value in turn...

    _.filer(list,function(c){}),主要用于根据条件进行过滤相关数据操作。参数说明:list为具体遍历的数组,function(c)为具体执行的过滤操作。需要返回具体的数据。

    var evens = _.filter([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
    => [2, 4, 6]

    _.map(list,function(c){}),主要用于根据元素进行相应的操作。参数说明:list为具体遍历的数组,function(c)为具体执行的对应的操作。需要返回具体的数据。

    _.map([1, 2, 3], function(num){ return num * 3; });
    => [3, 6, 9]
    _.map({one: 1, two: 2, three: 3}, function(num, key){ return num * 3; });
    => [3, 6, 9]

    _.reduce(list,function(c,m){var d   m.push(d)  return m},[]),主要用于对列表元素进行格式转换等处理操作,并生成全新结果并返回。参数说明:list为具体遍历的数组,function(c)为具体执行的组装数据的操作,并最终需要进行返回新的数据结果。

    var sum = _.reduce([1, 2, 3], function(memo, num){ return memo + num; }, 0);
    => 6

     _.find(list,function(c){}),主要用于根据条件查找相应的数据中匹配的对象。参数说明:list为具体遍历的数组,function(c)为具体查找的条件规则。

    var even = _.find([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
    => 2

    _.some(list,function(c){}),主要用于查找是否存在某个数据在数组中是否存在。参数说明:list为具体遍历的数组,function(c)为存在条件,返回true或false。

    _.some([null, 0, 'yes', false]);
    => true

    _.groupBy(list,param),主要用于根据分组字段对数据集合进行分组。参数说明:list为具体遍历的数组,param为分组字段或分组方法,使用单引号包含即可,返回具体分组后的数据。

    _.groupBy([1.3, 2.1, 2.4], function(num){ return Math.floor(num); });
    => {1: [1.3], 2: [2.1, 2.4]}
    
    _.groupBy(['one', 'two', 'three'], 'length');
    => {3: ["one", "two"], 5: ["three"]}

    _.sortBy(list,function(c){}),主要用于对数组进行排序。参数说明:list为具体遍历的数组,function(c)为具体排序的参数,默认为从小到大。

    _.sortBy([1, 2, 3, 4, 5, 6], function(num){ return Math.sin(num); });
    => [5, 4, 6, 3, 1, 2]

    _.indexBy(list,param),主要用于对数据进行索引。参数说明:list为具体遍历的数组,param为具体的索引字段或方法。

    var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
    _.indexBy(stooges, 'age');
    => {
      "40": {name: 'moe', age: 40},
      "50": {name: 'larry', age: 50},
      "60": {name: 'curly', age: 60}
    }

     _.values(list),主要用于将对象转换为数组。参数说明:list为具体的对象。

    _.values({one: 1, two: 2, three: 3});
    => [1, 2, 3]

     _.reduceRight(list,function(a,b){}),主要用于重新组装数据,但是与reduce不同的是,对数组的遍历是从末尾开始执行的。参数说明:list为具体的数据集合,function(a,b)为具体的组装的参数和输出参数。

    var list = [[0, 1], [2, 3], [4, 5]];
    var flat = _.reduceRight(list, function(a, b) { return a.concat(b); }, []);
    => [4, 5, 2, 3, 0, 1]

    _.where(list,{param}),主要用于过滤数据。参数说明:list为具体的数据集合,param为具体的过滤条件。

    _.where(listOfPlays, {author: "Shakespeare", year: 1611});
    => [{title: "Cymbeline", author: "Shakespeare", year: 1611},
        {title: "The Tempest", author: "Shakespeare", year: 1611}]

    _.findWhere(list,{param}),主要用于查找过滤操作。参数说明:list为具体的数据集合,param为具体的过滤条件。

    _.findWhere(publicServicePulitzers, {newsroom: "The New York Times"});
    => {year: 1918, newsroom: "The New York Times",
      reason: "For its public service in publishing in full so many official reports,
      documents and speeches by European statesmen relating to the progress and
      conduct of the war."}

    _.reject(list,function(c){}),主要用于丢弃规则数据的类似过滤操作。参数说明:list为具体的数据集合,function(c)为具体的丢弃规则。

    var odds = _.reject([1, 2, 3, 4, 5, 6], function(num){ return num % 2 == 0; });
    => [1, 3, 5]

    _.every(list,function(c){}),主要用于数据的匹配,要求每个元素必须匹配,缺一不可。参数说明:list为具体的数据集合,function(c)为具体的匹配的规则。

    _.every([true, 1, 'yes',!1],function(c){return c>1});
    false
    _.every([true, 1, 'yes',!1],function(c){return c=1});
    true

    _.contains(list,function(c){}),主要用于检查列表中是否包含元素。参数说明:list为具体的数据集合,function(c)为具体的包含的规则。

    _.contains([1, 2, 3], 3);
    => true

    _.invoke(list,methodName,*arguments),主要用于调用相关的方法。参数说明:list为具体的数据集合,可为多个数据集合执行同一个方法,methodName具体的执行的方法名称,arguments为具体的参数

    _.invoke([[5, 1, 7], [3, 2, 1]], 'sort');
    => [[1, 5, 7], [1, 2, 3]]

     _.pluck(list,params),主要用于列表中的对应属性的数据获取。参数说明:list为具体的数据集合,params为具体的属性名称。

    var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
    _.pluck(stooges, 'name');
    => ["moe", "larry", "curly"]

    _.max(list,function(c){}),主要用于列表数据的最大值的获取。参数说明:list为具体的数据集合,function(c)为具体的获取的最大值的规则。

    var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
    _.max(stooges, function(stooge){ return stooge.age; });
    => {name: 'curly', age: 60};

    _.max(list,function(c){}),主要用于列表数据的最小值的获取。参数说明:list为具体的数据集合,function(c)为具体的获取的最大值的规则。

    var numbers = [10, 5, 100, 2, 1000];
    _.min(numbers);
    => 2

    _.countBy(list,function(c){}),主要用于统计集合中的某个数量。参数说明:list为具体的数据集合,function(c)为具体的统计技术的规则。

    _.countBy([1, 2, 3, 4, 5], function(num) {
      return num % 2 == 0 ? 'even': 'odd';
    });
    => {odd: 3, even: 2}

    _.shuffle(list),主要用于对数据集合进行重新随机排序。参数说明:list为具体的数据集合。

    _.shuffle([1, 2, 3, 4, 5, 6]);
    => [4, 1, 6, 3, 5, 2]
    调用多次显示结果如下(从英文意思看,类似于洗牌的意思。):
    _.shuffle([1, 2, 3, 4, 5, 6]);
    [2, 5, 4, 3, 6, 1]
    _.shuffle([1, 2, 3, 4, 5, 6]);
    [5, 4, 2, 6, 3, 1]
    _.shuffle([1, 2, 3, 4, 5, 6]);
    [5, 1, 4, 6, 2, 3]

    _.sample(list,param),主要用于从对象集合中随机取出一个数据。参数说明:list为具体的数据集合,param可以为取出随机数的个数。

    _.sample([1, 2, 3, 4, 5, 6]);
    => 4
    
    _.sample([1, 2, 3, 4, 5, 6], 3);
    => [1, 6, 2]

    _.toArray(list),主要用于将对象转换为数组格式。参数说明:list为具体的数据集合。

    var stooges = [{name: 'moe', age: 40}, {name: 'larry', age: 50}, {name: 'curly', age: 60}];
    _.toArray(stooges);
    [Object, Object, Object]

    _.size(list),主要用于获取数据集合的大小。参数说明:list为具体的数据集合。

    _.size([null, 2, 3, 4, 5, 6]);
    6
    _.size([null, underfined, 3, 4, 5, 6]);
    ReferenceError: underfined is not defined
    _.size([null, underfined, 0, 4, 5, 6]);
    ReferenceError: underfined is not defined
    _.size([null, false,0, 4, 5, 6]);
    6

     数组操作

    _.union(*array),主要用于数组的联合,去除重复的数据。参数说明:array为具体的数组。

    _.union([1, 2, 3], [101, 2, 1, 10], [2, 1]);
    => [1, 2, 3, 101, 10]

    _.first(array,n),主要用于取出数组中的前n个数据。参数说明:array为具体的数组,n为取出的数据的个数。不填写n则为默认1个。

    _.first([5, 4, 3, 2, 1]);
    => 5

    _.initial(array,n),主要用于返回数组数据并去除末尾n个数。参数说明:array为具体的数组,n为要去掉末尾的数据个数。默认n为1个。

    _.initial([5, 4, 3, 2, 1]);
    => [5, 4, 3, 2]

    _.last(array,n),主要用于取出末尾n个数据。参数说明:array为具体的数组,n为要取出末尾的数据个数。默认n为1个。

    _.last([5, 4, 3, 2, 1]);
    => 1

    _.rest(array,index),主要用于在array中从index索引位置,开始取出数组的余下数据。参数说明:array为具体的数组,index为具体的开始索引位置。默认index为0

    _.rest([5, 4, 3, 2, 1]);
    => [4, 3, 2, 1]

    _.compact(array),主要用于对数组array进行false,0,null等值的过滤。参数说明:array为具体要过滤的数组。

    _.compact([0, 1, false, 2, '', 3]);
    => [1, 2, 3]

     _.uniq(array,property),主要用于对数组进行去除重复元素操作。参数说明:array为要去重复的数组,property为具体的字段名称。

    _.uniq([1, 2, 1, 3, 1, 4]);
    => [1, 2, 3, 4]
    
  • 相关阅读:
    CentOS下Redis 2.2.14安装配置详解(转载)
    centos 6.3 64位安装php5.5及配置tengine
    linux下安装php扩展redis缓存
    linux安装ruby ruby-devel rubygems bundler
    composer的create-project安装php框架laravel for mac教程
    Homebrew安装php5及composer for mac教程
    KeepAlive详解
    方便mac os 10.9系统中phpstorm配置php运行环境
    Android使用adb工具及root权限完成手机抓包
    linux服务器监控流量sh脚本
  • 原文地址:https://www.cnblogs.com/diaosizhang/p/3910063.html
Copyright © 2011-2022 走看看