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}]]
相关推荐
逐雨~1 天前
9.8C++作业
开发语言·c++
我爱挣钱我也要早睡!1 天前
Java 复习笔记
java·开发语言·笔记
Yang-Never1 天前
Kotlin协程 -> Job.join() 完整流程图与核心源码分析
android·开发语言·kotlin·android studio
牧羊狼的狼1 天前
React 中的 HOC 和 Hooks
前端·javascript·react.js·hooks·高阶组件·hoc
知识分享小能手1 天前
React学习教程,从入门到精通, React 属性(Props)语法知识点与案例详解(14)
前端·javascript·vue.js·学习·react.js·vue·react
luckys.one1 天前
第9篇:Freqtrade量化交易之config.json 基础入门与初始化
javascript·数据库·python·mysql·算法·json·区块链
TomCode先生1 天前
c#动态树形表达式详解
开发语言·c#
mCell1 天前
JavaScript 的多线程能力:Worker
前端·javascript·浏览器
高-老师1 天前
基于R语言的物种气候生态位动态量化与分布特征模拟
开发语言·r语言·物种气候
大翻哥哥1 天前
Python 2025:量化金融与智能交易的新纪元
开发语言·python·金融