博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
lodash(二)对象+循环遍历+排序
阅读量:7101 次
发布时间:2019-06-28

本文共 1449 字,大约阅读时间需要 4 分钟。

  • 前言:

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部分,却发现使用它可以快速解决自己之前遇到的很多问题,希望以后工作中可以应用自如。

转载地址:http://rkchl.baihongyu.com/

你可能感兴趣的文章
iOS程序的启动图片图标规范
查看>>
动画 -- 按钮 -- 左右晃动
查看>>
mysql+ssh整合样例,附源代码下载
查看>>
WWF3XOML方式创建和启动工作流 <第十篇>
查看>>
IE6 — 你若安好,便是晴天霹雳 [ 乱弹 ]
查看>>
组合数学 - 母函数的运用 --- 模板题
查看>>
检测MYSQL不同步发邮件通知的脚本
查看>>
Struts2学习笔记1
查看>>
python的ftp上传和下载
查看>>
ASP.NET MVC 中的路由
查看>>
微信公众平台帐号通过昵称无法搜索到怎么办
查看>>
Oracle笔记 六、PL/SQL简单语句块、变量定义
查看>>
Linux 常用命令
查看>>
何为蠕虫病毒
查看>>
[詹兴致矩阵论习题参考解答]习题7.3
查看>>
【BZOJ】1046: [HAOI2007]上升序列(dp)
查看>>
罗兰管弦乐音色表【中英文对照】 ----转载
查看>>
java操作数据库出现(][SQLServer 2000 Driver for JDBC]Error establishing socket.)的问题所在即解决办法...
查看>>
【LeetCode】102. Binary Tree Level Order Traversal (2 solutions)
查看>>
Uart串口与RS232串口的区别
查看>>