zoukankan      html  css  js  c++  java
  • lodash常用函数

    lodash常用函数 - Array、Collection

    lodash版本 v3.10.1

    1.Array、Collection

    pull

    移除数组中满足条件的元素

    var array = [1, 2, 3, 1, 2, 3];
    _.pull(array, 2, 3);
    // => [1, 1]
    

    slice

    从start位置到 end(但不包含end位置),截取 array数组

    _.slice([1, 2, 3, 1],2,3)
    // => [3]
    

    difference

    排除第一个数组中不包含第二个数组的值

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

    findIndex

    返回符合条件的元素的索引,没有找到返回-1

    var users = [
      { 'user': 'barney',  'active': false },
      { 'user': 'fred',    'active': false },
      { 'user': 'pebbles', 'active': true }
    ];
    _.findIndex(users, 'active', false);
    // => 0
    

    drop

    将数组中的前n(默认是1)个元素去掉,返回剩余部分

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

    xor

    取出各数组中不等的元素

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

    intersection

    取出各数组中全等的元素

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

    remove

    删掉满足条件的元素

    var array = [{name:'年龄',value:'34'},{name:'年龄',value:'20'},{name:'年龄',value:''}];
    _.remove(array, function(item) {
    	return item.value == '';
    })
    //[{name:'年龄',value:'34'},{name:'年龄',value:'20'}]
    

    union

    合并各数组的值,返回新数组

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

    uniq

    去除数组中的重复元素

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

    pluck

    取出json数组中某个属性的值,组成一个新数组返回

    var users = [
      { 'user': 'barney', 'age': 36 },
      { 'user': 'fred',   'age': 40 }
    ];
    
    _.pluck(users, 'user');
    // => ['barney', 'fred']
    

    zipObject

    由两个数组的元素配对组成一个新的对象

    _.zipObject(['fred', 'barney'], [30, 40]);
    // => { 'fred': 30, 'barney': 40 }
    

    filter

    以新数组形式返回数组中满足条件的元素

    _.filter([4, 5, 6], function(n) {
      return n % 2 == 0;
    });
    // => [4, 6]
    
    var users = [
      { 'user': 'barney', 'age': 36, 'active': true },
      { 'user': 'fred',   'age': 40, 'active': false }
    ];
    _.pluck(_.filter(users, 'active', false), 'user');
    // => ['fred']
    

    find

    返回数组中第一个满足条件的元素

    var users = [
      { 'user': 'barney',  'age': 36, 'active': true },
      { 'user': 'fred',    'age': 40, 'active': false },
      { 'user': 'pebbles', 'age': 1,  'active': true }
    ];
    
    _.result(_.find(users, function(chr) {
      return chr.age < 40;
    }), 'user');
    // => 'barney'
    
    // using the `_.matches` callback shorthand
    _.result(_.find(users, { 'age': 1, 'active': true }), 'user');
    // => 'pebbles'
    
    // using the `_.matchesProperty` callback shorthand
    _.result(_.find(users, 'active', false), 'user');
    // => 'fred'
    
    // using the `_.property` callback shorthand
    _.result(_.find(users, 'active'), 'user');
    // => 'barney'
    

    includes

    判断元素是否被包含在目标集合中

    _.includes([1, 2, 3], 1);
    // => true
    
    _.includes([1, 2, 3], 1, 2);
    // => false
    
    _.includes({ 'user': 'fred', 'age': 40 }, 'fred');
    // => true
    
    _.includes('pebbles', 'eb');
    // => true
    

    indexBy

    从集合中获取某一个属性值生成新对象,属性值为新对象的键

    var keyData = [
      { 'dir': 'left', 'code': 97 },
      { 'dir': 'right', 'code': 100 }
    ];
    
    _.indexBy(keyData, 'dir');
    // => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }
    

    sample

    从集合中随机获取n个元素,n默认为1

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

    size

    返回目标集合的长度

    _.size([1, 2, 3]);
    // => 3
    
    _.size({ 'a': 1, 'b': 2 });
    // => 2
    
    _.size('pebbles');
    // => 7
    

    sortBy

    根据特定规则对集合进行排序

    var users = [
      { 'user': 'fred' },
      { 'user': 'pebbles' },
      { 'user': 'barney' }
    ];
    
    _.pluck(_.sortBy(users, 'user'), 'user');
    // => ['barney', 'fred', 'pebbles']
    
  • 相关阅读:
    私有程序集的探测过程
    程序集版本控制
    浅谈对对象clone的理解
    [导入]WCF后传系列(3):深入WCF寻址Part 3—消息过滤引擎
    [导入]WCF后传系列(5):深入WCF寻址Part 5—逻辑地址和物理地址
    强名称程序集
    [导入]WCF后传系列(4):深入WCF寻址Part 4—自定义消息筛选器
    绑定过程小结
    概述
    UpdatePanel 的更新与触发环境
  • 原文地址:https://www.cnblogs.com/chenchenghua/p/7682326.html
Copyright © 2011-2022 走看看