zoukankan      html  css  js  c++  java
  • lodash常用方法

    有关Array的方法:

    1._.chunk(array, [size=1]),返回size长度数组组成的新数组

    _.chunk(['a', 'b', 'c', 'd'], 2);
    // => [['a', 'b'], ['c', 'd']]
     
    _.chunk(['a', 'b', 'c', 'd'], 3);
    // => [['a', 'b', 'c'], ['d']]
    2. _.compact(array), 返回去除falsenull0""undefinedNaN的数组
    _.compact([0, 1, false, 2, '', 3]);
    // => [1, 2, 3]

    3._.findIndex(array, [predicate=_.identity], [fromIndex=0]),返回迭代结果为true的第一个元素的索引

    var users = [
      { 'user': 'barney',  'active': false },
      { 'user': 'fred',    'active': false },
      { 'user': 'pebbles', 'active': true }
    ];
     
    _.findIndex(users, function(o) { return o.user == 'barney'; });
    // => 0
     
    // The `_.matches` iteratee shorthand.
    _.findIndex(users, { 'user': 'fred', 'active': false });
    // => 1
     
    // The `_.matchesProperty` iteratee shorthand.
    _.findIndex(users, ['active', false]);
    // => 0
     
    // The `_.property` iteratee shorthand.
    _.findIndex(users, 'active');
    // => 2

    4. _.flatten(array),摊平数组一个维度,返回新的数组

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

    5. _.head(array),返回数组第一个元素

    _.head([1, 2, 3]);
    // => 1
     
    _.head([]);
    // => undefined

    6._.union([arrays]), 返回只有唯一值得数组,返回新的数组

    _.union([2], [1, 2]);
    // => [2, 1]

    有关Collection的方法:

    1._.flatMap(collection, [iteratee=_.identity]), 对集合的每个元素迭代,摊平迭代后的结果,返回新的数组

    function duplicate(n) {
      return [n, n];
    }
     
    _.flatMap([1, 2], duplicate);
    // => [1, 1, 2, 2]

    2. _.groupBy(collection, [iteratee=_.identity]), 集合的元素经过迭代产生对象的key,key对应的value是产生key的那些元素,value是按顺序来的

    _.groupBy([6.1, 4.2, 6.3], Math.floor);
    // => { '4': [4.2], '6': [6.1, 6.3] }
     
    // The `_.property` iteratee shorthand.
    _.groupBy(['one', 'two', 'three'], 'length');
    // => { '3': ['one', 'two'], '5': ['three'] }

    3._.keyBy(collection, [iteratee=_.identity]), 集合的元素经过迭代产生对象的key,key对应的value是产生key的最后一个元素

    var array = [
      { 'dir': 'left', 'code': 97 },
      { 'dir': 'right', 'code': 100 },
      { 'dir': 'left11', 'code': 97 }
    ];
    
    _.keyBy(array, function(o) {
      return String.fromCharCode(o.code);
    });
    // => {a: Object {dir: "left11", code: 97}, d: Object {dir: "right", code: 100}}
    
    var array1 = [
      { 'dir': 'left', 'code': 97 },
      { 'dir': 'right', 'code': 100 }
    ];
    
    _.keyBy(array1, 'dir');
    // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }

    有关Function的方法:

    1._.debounce(func, [wait=0], [options={}]),调用的函数func,多久调用一次wait,提供option表示是否应在等待超时的前边或者后边调用

    jQuery(element).on('click', _.debounce(sendMail, 300, {
      'leading': true,
      'trailing': false
    }));

    2._.throttle(func, [wait=0], [options={}]),调用的函数func,多久调用一次wait,提供option表示是否应在等待超时的前边或者后边调用

    // Invoke `renewToken` when the click event is fired, but not more than once every 5 minutes.
    var throttled = _.throttle(renewToken, 300000, { 'trailing': false });
    jQuery(element).on('click', throttled);

    有关Lang的方法

    1._.clone(value),创建一个value的浅拷贝

    var objects = [{ 'a': 1 }, { 'b': 2 }];
     
    var shallow = _.clone(objects);
    console.log(shallow[0] === objects[0]);
    // => true

    2._.cloneDeep(value),深拷贝

    This method is like _.clone except that it recursively递归地 clones value.
    
    var objects = [{ 'a': 1 }, { 'b': 2 }];
     
    var deep = _.cloneDeep(objects);
    console.log(deep[0] === objects[0]);
    // => false

    有关Object的方法

    1._.omit(object, [paths]),忽略object里面paths里面的属性,返回带有剩下属性的对象,是新对象

    var object = { 'a': 1, 'b': '2', 'c': 3 };
     
    _.omit(object, ['a', 'c']);
    // => { 'b': '2' }

    2._.pick(object, [paths]),返回带有paths里面属性的对象,是新对象

    var object = { 'a': 1, 'b': '2', 'c': 3 };
     
    _.pick(object, ['a', 'c']);
    // => { 'a': 1, 'c': 3 }

    有关String的方法

    1._.camelCase([string='']),返回驼峰形式的

    _.camelCase('Foo Bar');
    // => 'fooBar'
     
    _.camelCase('--foo-bar--');
    // => 'fooBar'
     
    _.camelCase('__FOO_BAR__');
    // => 'fooBar'

    2._.snakeCase([string='']),返回下划线形式的

    _.snakeCase('Foo Bar');
    // => 'foo_bar'
     
    _.snakeCase('fooBar');
    // => 'foo_bar'
     
    _.snakeCase('--FOO-BAR--');
    // => 'foo_bar'

    3._.trim([string=''], [chars=whitespace]),移除空格或者特定字符

    _.trim('  abc  ');
    // => 'abc'
     
    _.trim('-_-abc-_-', '_-');
    // => 'abc'
     
    _.map(['  foo  ', '  bar  '], _.trim);
    // => ['foo', 'bar']

    有关Util的方法

    1._.noop(),返回undefined

    _.times(2, _.noop);
    // => [undefined, undefined]

    2._.times(n, [iteratee=_.identity]), 返回调用迭代器n次生成的结果构成的数组

    _.times(3, String);
    // => ['0', '1', '2']
     
     _.times(4, _.constant(0));
    // => [0, 0, 0, 0]
  • 相关阅读:
    DataTable轉EXCEL 3/21
    中風預防知識
    unable to convert mysql date/time value to system.data.time 11/14
    win8 获得地理坐标 GIS
    页面嵌套 GIS
    win8 metro 弹出一个部分 GIS
    正则表达式基础 之 ? GIS
    windows phone pivot 开发过程中的使用心得 GIS
    线程不安全 GIS
    线程基础知识 GIS
  • 原文地址:https://www.cnblogs.com/dylAlex/p/14869454.html
Copyright © 2011-2022 走看看