Lodash工具库小记
项目中可以用工具安装
npm install lodash
# 或者
pnpm install lodash
// 引入整个Lodash
import _ from 'lodash';
// 只引入需要的模块
import { debounce } from 'lodash';
或者直接cdn引入
<script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>
differenceWith返回一个过滤值后的新数组
let arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
let temp = [1, 2, 3, 4, 5]
let reverseArr = _.differenceWith(arr, temp)
console.log(reverseArr)
//得到结果[6, 7, 8, 9, 10]
//第三个参数,可以回调函数
orderBy对对象数组以指定字段进行排序
let arrOrder = [{ id: 1, name: 'n' }, { id: 3, name: 'm' }, { id: 2, name: 'a' }, { id: 5, name: 'e' }]
let newArr = _.orderBy(arrOrder, ['id'], ['asc']);
console.log(newArr)
//得到结果[
// {id: 1, name: 'n'},
// {id: 2, name: 'a'},
// {id: 3, name: 'm'},
// {id: 5, name: 'e'},
// ];
uniqby可以用于通过主键去重
let arrUniq = [
{id:1,value:'q'},
{id:2,value:'w'},
{id:3,value:'e'},
{id:1,value:'q'},
{id:2,value:'w'},
{id:3,value:'e'},
]
let newUniq = _.uniqBy(arrUniq,'id')
console.log(newUniq)
//得到结果[
//{id:1,value:'q'},
//{id:2,value:'w'},
//{id:3,value:'e'},
//]
max取数组最大值
let result = _.max([4, 2, 8, 6]);
console.log(arr)
//得到结果8
debounce防抖
onChange:_.debounce( e => {
formModel.jsje = Number(e) * Number(formModel.jssl)
},500)