• Lodash-一个好用的JavaScript工具库


    本文不再更新,可能存在内容过时的情况,实时更新请访问原地址:Lodash-一个好用的JavaScript工具库

    Lodash是一个JavaScript库,它使用函数式编程范例为常见的编程任务提供实用程序功能。下面提供了几个函数的使用示例。

    数据示例

    新建一个obj对象进行测试。

        var obj = [{
                "name": "Michael",
                "age": "25",
                "gender": "man",
                "hobby": [
                    "Basketball", "Running"
                ]
            },
            {
                "name": "Jack",
                "age": "23",
                "gender": "man",
                "hobby": [
                    "Basketball", "Singing"
                ]
            },
            {
                "name": "Lucy",
                "age": "27",
                "gender": "woman",
                "hobby": [
                    "Dancing", "Running"
                ]
            }
        ];
    
        var obj2 = [{
            "name": "Sky",
            "age": "27",
            "gender": "woman",
            "hobby": [
                "Dancing", "Running"
            ]
        }];
    

    用法示例

    1._.filter

    使用说明:对数组进行过滤,只返回符合条件的元素。

    例,过滤genderman的元素。

    var boys = _.filter(obj, ['gender', 'man']); 
    

    结果:

    [
      {
        "name": "Michael",
        "age": "25",
        "gender": "man",
        "hobby": [
          "Basketball",
          "Running"
        ]
      },
      {
        "name": "Jack",
        "age": "23",
        "gender": "man",
        "hobby": [
          "Basketball",
          "Singing"
        ]
      }
    ]
    

    2._.some

    使用说明:检查集合中是否有符合指定条件的元素,有则返回true。

    例,是否存在名为Lucy的女性。

    var checkSome = _.some(obj, { "name": "Lucy", "gender": "woman" });
    

    结果:

    true
    

    3._.map

    使用说明:根据参数对集合进行处理并返回新数组。

    例,获取name属性组成的数组或者namegender组成的数组。

    var names = _.map(obj, 'name');
    
    var result = _.map(obj, function(e){
           return {"name":e.name,"gender":e.gender}
        });
    

    结果:

    ["Michael","Jack","Lucy"]
    
    [{"name":"Michael","gender":"man"},{"name":"Jack","gender":"man"},{"name":"Lucy","gender":"woman"}]
    

    4._.union

    使用说明:合并多个JS数组。

    例,合并obj、obj2两个数组。

    var resultArray = _.union(obj, obj2);
    console.log(JSON.stringify(resultArray));
    

    结果:

    [
      {
        "name": "Michael",
        "age": "25",
        "gender": "man",
        "hobby": [
          "Basketball",
          "Running"
        ]
      },
      {
        "name": "Jack",
        "age": "23",
        "gender": "man",
        "hobby": [
          "Basketball",
          "Singing"
        ]
      },
      {
        "name": "Lucy",
        "age": "27",
        "gender": "woman",
        "hobby": [
          "Dancing",
          "Running"
        ]
      },
      {
        "name": "Sky",
        "age": "27",
        "gender": "woman",
        "hobby": [
          "Dancing",
          "Running"
        ]
      }
    ]
    

    5._.includes

    使用说明:检查集合中是否包含某值。

    例,数组中是否存在sss元素。

    var array = ['sss','bbb'];
    var check = _.includes(array, 'sss'); // true
    

    结果:

    true
    

    6._.uniq

    使用说明:数组去重。

    例,去重。

    _.uniq([2, 1, 2]);
    

    结果:

    [2, 1]
    

    7._.chunk

    使用说明:数组分块,按指定数量均分数组。

    例,分块,每2个分为拆分为一个数组。

    _.chunk(['a', 'b', 'c', 'd'], 2);
    _.chunk(['a', 'b', 'c', 'd'], 3);
    

    结果:

    [['a', 'b'], ['c', 'd']]
    
     [['a', 'b', 'c'], ['d']]
    
  • 相关阅读:
    hdu 5071
    HDU4945 2048(dp)
    ZOJ3229 Shoot the Bullet(有源汇的上下界最大流)
    HDU Destroy Transportation system(有上下界的可行流)
    Treap模板
    HDU4916 Count on the path(树dp??)
    HDU2829 Lawrence(斜率优化dp)
    HDU3507 Print Article(斜率优化dp)
    POJ3164 Command Network(最小树形图)
    Codeforces 452E Three Strings(后缀自动机)
  • 原文地址:https://www.cnblogs.com/cobcmw/p/12718195.html
Copyright © 2020-2023  润新知