您现在的位置是:首页 > 文章详情

Lodash 扩展JS通用方法

日期:2018-09-25点击:312
版权声明:本文首发 http://asing1elife.com ,转载请注明出处。 https://blog.csdn.net/asing1elife/article/details/82848345

Lodash是一个著名的javascript原生库,不需要引入其他第三方依赖。是一个意在提高开发者效率,提高JS原生方法性能的JS库

更多精彩

官网

  1. Lodash Documentation
  2. lodash 中文文档

语法

集合[Collection]

  1. _.find 在集合中获取到指定元素
var users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': true } ] // 自定义匹配规则 _.find(users, function(o) { return o.age < 40; }) 
  1. _.filter 获取集合中满足条件的元素集
var users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false } ] // => { 'user': 'fred', 'age': 40, 'active': false } _.filter(users, function(o) { return !o.active; }) 

数组[Array]

  1. _.findIndex 在数组中获取指定元素的索引
    • 匹配的规则可自定义
    • 匹配到则返回对应索引,否则返回-1
let fileType = currentFile.type let currentIndex = _.findIndex(this.types, function (type) { return fileType.toLowerCase().match(new RegExp(type)) }) 
  1. _.unionWith 连接两个数组
    • 连接的规则可自定义
    • 规则返回true的则跳过
this.examPaper.examQuestions = _.unionWith(this.examPaper.examQuestions, this.selectQuestions, (a, b) => { return a.hexId === b.hexId }) 
  1. _.drop 移除数组元素
    • 默认移除第一个元素
    • 可显式指定从第几个元素开始移除
_.drop(this.userWorkIds) 
原文链接:https://yq.aliyun.com/articles/646337
关注公众号

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。

持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。

转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。

文章评论

共有0条评论来说两句吧...

文章二维码

扫描即可查看该文章

点击排行

推荐阅读

最新文章