https://www.bootcdn.cn/lodash.js/
将数组进行切分
var arr = [1,2,3,4]
var newArr = _.chunk(arr,2)
console.log(newArr) //[1,2] [3,4]
去除假值。(将所有的空值,0,NaN过滤掉)
_.compact(['1','2',' ',0])
// => ['1','2']
数组去重。(将数组中的对象去重,只能是数组去重,不能是对象去重。)
_.uniq([1,1,3])
// => [1,3]
根据条件去除某个元素
var foo = [
{id: 0, name: "aaa", age: 33},
{id: 1, name: "bbb", age: 25}
]
var bar = _.reject(foo, ['id', 0])
//bar = [{id: 1, name: "bbb", age: 25}]
无序化
_.shuffle([1,2,3,4]);// 无序化
根据key来筛选数组
var foo = {id: 0, name: "aaa", age: 33}
var bar = _.pick(foo, ['name', 'age'])
//bar = {name: "aaa", age: 33}
返回object中的所有key
var foo = {id: 0, name: "aaa", age: 33}
var bar = _.keys(foo)
//bar = ['id', 'name', 'age']
深度拷贝
_.forEach(agent,function(n,key) {
agent[key].agent_id= agent[key].agent_name
})
这是一个常见的forEach的数组遍历,使用了lodash过后,_.forEach()这是一个值,而不是一个函数。就可以直接
const arr = _.forEach();
这时候arr就是新的数组agent
merge 参数合并
递归的将源对象和继承的可枚举字符串监控属性合并到目标对象中。源对象从左到右引用,后续来源将覆盖以前来源的属性分配。
var object = {
'a': [{ 'b': 2 }, { 'd': 4 }]
};
var other = {
'a': [{ 'c': 3 }, { 'e': 5 }]
};
_.merge(object, other);
// => { 'a': [{ 'b': 2, 'c': 3 }, { 'd': 4, 'e': 5 }]
在实际开发中,前端在接口的请求可以merge一下之前的query和现在改变的查询的值,再去请求后端接口的数据
console.log(_.random(10, 20)); // 获取随机数
_.random(15, 20, true); // 随机浮点
_.sample(["lisong", "heyan"], 1);// 随机获取数组中的某一项,attr2:随机获取的个数
find查找数组
var foo = [
{id: 0, name: "aaa", age: 33},
{id: 1, name: "bbb", age: 25}
]
var bar = _.find(foo, ['id', 0])
//bar = {id: 0, name: "aaa", age: 33}
注意一下如果没找到的话,会返回undefined,要处理一下
以某个属性为键,将数组转为对象
var foo = var foo = [
{id: 0, name: "aaa", age: 33},
{id: 1, name: "bbb", age: 25}
]
var bar = _.keyBy(foo, 'name')
//bar = {
// aaa: {id: 0, name: "aaa", age: 33},
// bbb: {id: 1, name: "bbb", age: 25}
//}
根据条件过滤出符合条件的元素,返回新数组
var foo = var foo = [
{id: 0, name: "aaa", age: 33},
{id: 1, name: "bbb", age: 25}
]
var bar = _.filter(foo, ['name', "aaa"])
//bar = {
// aaa: {id: 0, name: "aaa", age: 33}
//}
从集合中挑出一个key,将其值作为数组返回
var foo = var foo = [
{id: 0, name: "aaa", age: 33},
{id: 1, name: "bbb", age: 25}
]
var bar = _.map(foo, 'name')
//bar = ["aaa", "bbb"]
数组中最大值、最小值、数组求和
var foo = [1, 2, 3, 4]
var bar = _.max(foo)
//bar = 4
bar = _.min(foo)
//bar = 1
bar = _.sum(foo)
//bar = 10
在两端、开头、末尾补齐字符
var foo = "helloworld"
var bar = _.pad(foo, 14, '-')
//bar = --helloworld--
bar = _.padStart(foo, 14, '-')
//bar = ----helloworld
bar = _.padEnd(foo, 14, '-')
//bar = helloworld----
var foo = [
{id: 0, name: "aaa", age: 33},
{id: 1, name: "bbb", age: 25}
]
var bar = _.find(foo, ['id', _.max(_.map(foo, 'id'))])
// bar = {id: 1, name: "bbb", age: 25}
// ps:也可以用maxBy某个key来代替
var foo = [
{id: 0, name: "aaa", age: 33},
{id: 1, name: "bbb", age: 25}
]
let list = _.keyBy(foo, 'id')
list[0].name = "ccc"
var bar = _.map(list)
// bar = [
// {id: 0, name: "ccc", age: 33},
// {id: 1, name: "bbb", age: 25}
//]
var arr = [1,2,[3,4,[5],6]]
var a8 = _.flatten(arr) //[ 1, 2, 3, 4, [ 5 ], 6 ]
var a9 = _.flattenDeep(arr) //[ 1, 2, 3, 4, 5, 6 ]
var a10 = _.flattenDepth(arr,2) //[ 1, 2, 3, 4, 5, 6 ]
var arr2 = [12,14,11,12,12,1,14,16,17,22,2,11,12]
var a11 = Array.from(new Set(arr2))
var a12 = [...new Set(arr2)]
var a13 = _.uniq(arr2) //[ 12, 14, 11, 1, 16, 17, 22, 2 ]
var arrObj = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 },{ 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];
var a14 = _.uniqWith(arrObj, _.isEqual) //[ { x: 1, y: 2 }, { x: 2, y: 1 } ]