zoukankan      html  css  js  c++  java
  • lodash(二)对象+循环遍历+排序

    • 前言:

    lodash(一)中只是研究了array中的多种方法,接下来就是经常用到的循环遍历问题

    • 过程:

    1._.forEach(collection, [iteratee=_.identity], [thisArg])  遍历

        _.forEach([22,33,11,55],function (value) {//若一个参数,返回的便是其value值
          console.log(value);//22 33 11 55
        });
        _.forEach([22,33,11,55],function (value,index) {//这里规定的就是第一个参数返回的是value值,第二个参数是下标index
          console.log(value);
        });

    2._.sortBy(collection, [iteratee=_.identity], [thisArg])   排序匿名函数+字符串

        var arr = [
            {name: 'bb',age:23},
            {name: 'aa',age:22}
        ];
    
        var arrSortResult = _.sortBy(arr, function(item){
            return item.name;
        });
        _.forEach(arrSortResult, function(item){
            console.log(item.name); //aa bb
        });
        var strSortResult = _.sortBy('cda').join('');//join()方法 方法用于把数组中的所有元素放入一个字符串。元素是通过指定的分隔符进行分隔的。
         console.log(strSortResult);//acd

    3._.sortedIndex(array, value, [iteratee=_.identity], [thisArg]) 

             参数:array (Array): 需要检查的数组    value (*): 插入的判断参数    [iteratee=_.identity] (Function|Object|string): 遍历方法   [thisArg] (*): iteratee的绑定值

        var collection = ['a', 'b', 'c', 'd', 'f'];
        console.log('before: ' + collection.join(' '));//before: a b c d f
        var toBeInserted = 'e';
        var sortedIndex = _.sortedIndex(collection, toBeInserted);
        console.log("This is sortedIndex:"+sortedIndex);//This is sortedIndex:4
        collection.splice(sortedIndex, 0, toBeInserted);
        console.log('after:' + collection.join(' '));//after:a b c d e f
    • 后言:

    只是简单了解了一下关于lodash部分,却发现使用它可以快速解决自己之前遇到的很多问题,希望以后工作中可以应用自如。

  • 相关阅读:
    racket eval
    mex不兼容
    【转】雷军 程序员随想
    UBoot 目录结构和编译过程
    远程监控web开发
    STL容器[08]
    STL容器[07]
    STL容器[17]
    STL容器[06]
    stl.map使用总结
  • 原文地址:https://www.cnblogs.com/zhengyeye/p/6741081.html
Copyright © 2011-2022 走看看