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}]]
相关推荐
WWJA王文举25 分钟前
FreeRTOS事件组和任务通知详解:为什么任务通知比队列更轻量?
开发语言·php
宿67427 分钟前
vue3-config
前端·javascript·vue.js
wuyk5551 小时前
12.归并排序:分治思想的稳定排序算法
开发语言·数据结构·算法·排序算法
吴声子夜歌1 小时前
ApacheCommons——commons-text(模板替换与文本算法)
java·开发语言·算法·apache
quantdash_cc2 小时前
量化数据源怎么选:稳定性、实时性和数据完整性到底该如何权衡?
开发语言·python·数据分析·量化交易·股票数据·quantdash
萧瑟余晖2 小时前
Java深入解析篇五十四之对象模型详解
java·开发语言
张文是假的啊3 小时前
Java | record | Controller逻辑
java·开发语言
不甘先生3 小时前
Go 中 type、方法与指针接收者:从 str_name.Name() 看懂 Go 的类型系统
开发语言·后端·golang
ShineWinsu3 小时前
对于 Vue 3:从为什么学 Vue,到声明式渲染与数据响应式的解析
前端·javascript·vue.js
quantdash_cc4 小时前
Python 股票 K 线数据质量校验:字段、缺失值、重复行和价格异常
开发语言·python·数据分析·量化交易·股票数据·quantdash