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}]]
相关推荐
用户0763767107461 分钟前
JavaScript遍历数组的方法总结
javascript
野生的程序媛5 分钟前
重生之我在学Vue--第7天 Vue 3 数据请求(Axios)
前端·javascript·vue.js
蘑菇王13 分钟前
无需打包构建?ESM Bundleless 开发的探索与实践
前端·javascript
秋天的一阵风32 分钟前
‌ES Module 都过十岁生日了,你还不了解它的运行原理吗?
前端·javascript·面试
不爱学习的小枫1 小时前
scala的集合
开发语言·scala
梦醒沉醉1 小时前
Scala的初步使用
开发语言·后端·scala
小白学大数据1 小时前
Fuel 爬虫:Scala 中的图片数据采集与分析
开发语言·爬虫·scala
Moment1 小时前
前端白屏检测SDK:从方案设计到原理实现的全方位讲解 ☺️☺️☺️
前端·javascript·面试
阿波次嘚1 小时前
关于在electron(Nodejs)中使用 Napi 的简单记录
前端·javascript·electron
接着奏乐接着舞。1 小时前
Electron + Vue 项目如何实现软件在线更新
javascript·vue.js·electron