lodash 对象+循环遍历+排序

40 篇文章 0 订阅
2 篇文章 1 订阅

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

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值