Lodash.js处理数组、对象、函数等常用方法介绍

参考网址:Lodash 简介 | Lodash中文文档 | Lodash中文网

安装

npm install lodash

引入

const _ = require('lodash')

使用

基础数据

javascript 复制代码
const array1 = [{ 'id': 1, 'value': 'A' }, { 'id': 2, 'value': 'B' }]
const array2 = [{ 'id': 2, 'value': 'B' }, { 'id': 3, 'value': 'C' }]

交集:既属于array1也属于array2

javascript 复制代码
const intersection = _.intersectionWith(array1, array2, _.isEqual);
console.log(intersection)
//输出 [{id: 2,value: 'B'}]

其中第三个参数也可以自定义,比如

javascript 复制代码
// 自定义比较函数,比较对象的id
const isEqual = (obj1, obj2) => obj1.id === obj2.id;

等同于

javascript 复制代码
const intersection = _.intersectionBy(array1, array2, 'id');
console.log(intersection)
//输出 [{id: 2,value: 'B'}]

_.intersection()操作简单数组

差集:属于array1不属于array2

javascript 复制代码
const difference = _.differenceWith(array1, array2, _.isEqual)
console.log(difference)
// 输出: [ { id: 1, value: 'A' } ]

_.differenceBy()同上

_.difference()操作简单数组

并集:array1和array2合集并去重

javascript 复制代码
const union = _.unionWith(array1, array2, _.isEqual)
console.log(union)
//输出 [{id: 1, value: 'A'},{id: 2, value: 'B'},{id: 3, value: 'C'}]

_.unionBy()同上

_.union()操作简单数组

找出array1和array2的不同元素

javascript 复制代码
const xor = _.xorWith(array1, array2, _.isEqual);
console.log(xor)
//输出 [{id: 1,value: 'A'},{id: 3,value:'C'}]

_.xorBy()同上

_.xor()操作简单数组

最大值:

javascript 复制代码
const max = _.maxBy(array1, 'id');
console.log(max)
//输出 {id: 2,value: 'B'}

求和:

javascript 复制代码
const sum = _.sumBy(array1, 'id');
console.log(sum)
//输出 3

排序

javascript 复制代码
const array1 = [{ 'id': 1, 'value': 'A', age: 54 }, { 'id': 2, 'value': 'B', age: 66 }, { 'id': 3, 'value': 'B', age: 23 }]

//升序
const asc = _.sortBy(array1, ['value','age'])
console.log(asc)
//输出 [{id: 3, value: 'B', age: 23},{id: 1, value: 'A', age: 54},{id: 2, value: 'B', age: 66}]

//降序
const desc= _.sortBy(array1, ['value','age']).reverse()
console.log(desc)
//输出 [{id: 2, value: 'B', age: 66},{id: 1, value: 'A', age: 54},{id: 3, value: 'B', age: 23}]

const users = [
  { 'user': 'fred',   'age': 48 },
  { 'user': 'barney', 'age': 34 },
  { 'user': 'fred',   'age': 40 },
  { 'user': 'barney', 'age': 36 }
];
 
// 以 `user` 升序排序 再  `age` 以降序排序。
_.orderBy(users, ['user', 'age'], ['asc', 'desc']);
//输出 [['barney', 36], ['barney', 34], ['fred', 48], ['fred', 40]]

集合中的元素是否存在 任意 truthy(真值)的元素

javascript 复制代码
const exist = _.some(array1, ['value', 'A'])
//输出 true
const exist = _.some(array1, { 'value': 'A' })
//输出 true
const exist = _.some(array1, { 'id': 1, 'value': 'A' })
//输出 true

const exist = _.some(array1, ['value', 'C'])
//输出 false
const exist = _.some(array1, { 'value': 'C' })
//输出 false
const exist = _.some(array1, { 'id': 1, 'value': 'C' })
//输出 false

按照某个条件将一个数组分为两个

javascript 复制代码
//将value值是B的分为一个数组,不是B的分为一组
_.partition(array1, { 'value': 'B' })
//输出 [[{"id":2,"value":"B","age":66},{"id":3,"value":"B","age":23}],[{"id":1,"value":"A","age":54}]]
相关推荐
Python私教19 分钟前
YggJS RLogin暗黑霓虹主题登录注册页面 版本:v0.1.1
开发语言·javascript·ecmascript
carver w1 小时前
MFC,C++,海康SDK,回调,轮询
开发语言·c++·mfc
南半球与北海道#1 小时前
el-table合并单元格
javascript·vue.js·elementui·表格合并
王廷胡_白嫖帝1 小时前
Qt猜数字游戏项目开发教程 - 从零开始构建趣味小游戏
开发语言·qt·游戏
Jimmy1 小时前
客户端存储 - IndexedDB
前端·javascript·indexeddb
XH华1 小时前
C语言第九章字符函数和字符串函数
c语言·开发语言
一个会的不多的人1 小时前
C# NX二次开发:操作按钮控件Button和标签控件Label详解
开发语言·c#
gzzeason1 小时前
ES6+内置进制转换方法
前端·ecmascript·es6
成小白1 小时前
前端实现两个页面之间的通讯
前端·javascript
啷咯哩咯啷1 小时前
element-plus el-tree-v2大数据量勾选节点卡顿问题
前端·javascript·vue.js