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}]]
相关推荐
林希_Rachel_傻希希7 小时前
web性能优化之————图片效果
前端·javascript·面试
橘子星7 小时前
基于 MCP 协议实现本地文件读取工具服务开发实践
javascript·人工智能
Darling噜啦啦7 小时前
前端存储与 this 指向完全指南:从 LocalStorage 实战到 call/apply/bind 深度解析
前端·javascript
sugar__salt7 小时前
手撕字符串算法:反转、回文、验证回文 Ⅱ 完整拆解
javascript·算法·面试·职场和发展
To_OC7 小时前
从一行报错开始,把字符串反转、回文算法连带着包装类一起捋明白
javascript·算法·api
zhiSiBuYu05177 小时前
重排序(Rerank)提升检索准确率实战指南
开发语言·python·算法
蜡台8 小时前
Node 安装 awesome-qr 失败解决
javascript·vue·qrcode·awesome-qr
c++之路8 小时前
C++跨平台(九):跨平台字节序统一处理
开发语言·arm开发·c++
Evand J8 小时前
【MATLAB例程|车联网6】考虑调头车流扰动与网联车辆实时感知信息的干线多交叉口 FAC-CV 全感应协调控制仿真与性能对比分析
开发语言·matlab·仿真·代码·车联网·智慧交通·车辆