const _ = require('lodash')
lodash.each
_([1, 2]).forEach(function(value) {
console.log(value);
});
// `1`
// `2`.
_.forEach({ 'a': 1, 'b': 2 }, function(value, key) {
console.log(key);
});
assign merge
const aa = _.assign({},{a:1},{a:2},{b:3})
//{a:2,b:3}
const bb = _.merge({},{a:1},{a:2},{b:3})
//{a:2,b:3}
const a1 = _.assign({},{a:1},{b:{a:1,b:2}},{b:{a:3}})
//{a:1,b:{a:3}}
const a2 = _.merge({},{a:1},{b:{a:1,b:2}},{b:{a:3}})
//{a:1,b:{a:3,b:2}}
mergeWith
const object = { 'a': [3], 'b': [5] };
const other = { 'a': [6], 'b': [7] };
function foo (a,b) {
if (_.isArray(a)) {
return a.concat(b)
}
}
function foo5 (a,b) {
return _.toNumber(a) + _.toNumber(b)
}
const a3 = _.merge(object,other)
//{'a':[6],'b':[7]}
const a4 = _.mergeWith(object,other)
//{'a':[6],'b':[7]}
const a5 = _.mergeWith(object,other,foo)
//{'a':[3,6],'b':[5,7]}
const aaa = _.mergeWith(object,other,foo5)
//{'a':9,'b':12}
sumBy
const arrs = [{ 'n': 4 }, { 'n': 2 }, { 'n': 8 }, { 'n': 6 }];
const a6 = _.sumBy(arrs, k => k.n) //20
const aa6 = _.sumBy(arrs,'n') //20
// console.log(aa6);
const ccc = _.sum([3,16,15,20])
// console.log(ccc);
sortBy
const users = [
{ 'user': 'barney', 'age': 36 },
{ 'user': 'fred', 'age': 40 },
{ 'user': 'pebbles', 'age': 1 }
];
const youngest = _
.chain(users)
.sortBy('age')
.value()
console.log(youngest);
//[ { user: 'pebbles', age: 1 },
// { user: 'barney', age: 36 },
// { user: 'fred', age: 40 } ]
// _.chain(arr)
// LodashWrapper {
// __wrapped__: [ { user: 'barney', age: 36 },
// { user: 'fred', age: 40 },
// { user: 'pebbles', age: 1 } ],
// __actions__: [],
// __chain__: true,
// __index__: 0, __values__: undefined }
const a7 = _.sortBy(users,'age')
flatten
const arr = [1,2,[3,4,[5],6]]
const a8 = _.flatten(arr) //[ 1, 2, 3, 4, [ 5 ], 6 ]
const a9 = _.flattenDeep(arr) //[ 1, 2, 3, 4, 5, 6 ]
const a10 = _.flattenDepth(arr,2) //[ 1, 2, 3, 4, 5, 6 ]
- 数组去重
const arr2 = [12,14,11,12,12,1,14,16,17,22,2,11,12]
const a11 = Array.from(new Set(arr2))
const a12 = [...new Set(arr2)]
const a13 = _.uniq(arr2) //[ 12, 14, 11, 1, 16, 17, 22, 2 ]
const arrObj = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 },{ 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];
const a14 = _.uniqWith(arrObj, _.isEqual) //[ { x: 1, y: 2 }, { x: 2, y: 1 } ]
isEqual
const object = { 'a': 1, 'b':2};
const other = { 'a': 1,'b':2 };
const a15 = _.isEqual(object, other); //true
const a16 = _.isEqual([1,[2],2],[1,[2],2]) //true
- 其他
const a17 = _.random(1, 5,true);
// console.log(a17);
const a18 = _.inRange(5,1,10) //true
const a19 = _.clamp(90, 5, 100) //90
const a20 = _.min([14,12,11,15,14,22,10,34,12]) //10
const objects = [{ 'n': 4 }, { 'n': 2 },{'n':5},{'n':3}];
const a21 = _.minBy(objects,'n') //{ n: 2 }
// _.maxBy()
const a22 = _.minBy(objects,o => o.n) //{ n: 2 }
console.log(a22);
- 深拷贝
_.cloneDeep()
const original = { foo: "bar" };
const newObj = _.cloneDeep(original)
original.foo = 'aaa'
console.log(original); //{ foo: 'aaa' }
console.log(newObj); //{ foo: 'bar' }
- 浅拷贝
_.clone()
const original1 = { foo: "bar" };
const newObj1 = _.clone(original)
original1.foo = 'aaa'
console.log(original1); //{ foo: 'aaa' }
console.log(newObj1); //{ foo: 'aaa' }
reduce
const users = [
{ name: "John", age: 30 },
{ name: "Jane", age: 28 },
{ name: "Bill", age: 65 },
{ name: "Emily", age: 17 },
{ name: "Jack", age: 30 }
]
const finalRes = _.reduce(users,(result,user) => {
if (user.age > 17 && user.age< 66) {
(result[user.age] || (result[user.age] = [])).push(user)
}
return result
},{})
// { '28': [ { name: 'Jane', age: 28 }],
// '30': [ { name: 'John', age: 30 }, { name: 'Jack', age: 30 } ],
// '65': [ { name: 'Bill', age: 65 }]
// }
- 返回一个新的联合数组。去重
const b1 = _.union([1],[1,2],[1,2,3],[3,6,9]) //[ 1, 2, 3, 6, 9 ]
const bb2 = _.union([1,2,3,1,2,1,11,2,3]) //[ 1, 2, 3, 11 ]
const b2 = _.unionBy([2.1], [1.2, 2.3,2.4,3.1], Math.floor); //[ 2.1, 1.2, 3.1 ]
const b3 = _.unionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 },{'x':3}],[{'x':2}] ,'x');
//[ { x: 1 }, { x: 2 }, { x: 3 } ]
const b4 = _.unionBy([{'x':1},{'x':2},{'x':3},{'x':3},{'x':1}],'x')
//[ { x: 1 }, { x: 2 }, { x: 3 } ]
const b5 = _.unionBy([{'x':1},{'x':2},{'x':3},{'x':3},{'x':1}],a=> a.x)
//[ { x: 1 }, { x: 2 }, { x: 3 } ]
const b6 = _.uniq([1],[1,2],[1,2,3],[3,6,9]) //1
const b7 = _.uniq([1,2,3,1,2,1,11,2,3]) //[ 1, 2, 3, 11 ]
const b8 = _.uniqBy([{ 'x': 2 }, { 'x': 1 },{'x':3},{'x':1},{'x':2}],'x')
//[ { x: 2 }, { x: 1 }, { x: 3 } ]
总结:union和uniq的区别:union支持多个数组去重,uniq只能是一个数组
-
fromPairs
和toPairs
const b9 = _.fromPairs([['fred', 30], ['barney', 'a'],['some',1],['a','c']]);
//{ fred: 30, barney: 'a', some: 1, a: 'c' }
const b10 = _.toPairs({fred: 30, barney: 'a', some: 1, a: 'c' })
const b11 = Object.entries({fred: 30, barney: 'a', some: 1, a: 'c' })
//[ [ 'fred', 30 ], [ 'barney', 'a' ], [ 'some', 1 ], [ 'a', 'c' ] ]
mapValues
const users = {
'fred': { 'user': 'fred', 'age': 40 },
'pebbles': { 'user': 'pebbles', 'age': 1 }
};
const c1 = _.mapValues(users,'age') //{ fred: 40, pebbles: 1 }
16 pickBy
const object = { 'a': 1, 'b': '2', 'c': 3 };
const c2 = _.pick(object,['a','c']) //{ a: 1, c: 3 }
const c3 = _.pickBy(object, _.isString) //{ b: '2' }
console.log(c3);
drop
const c3 = _.drop([1, 2, 3], 2); //[ 3 ]
console.log(c3);
shuffle()
const c4 = _.shuffle([1,5,11,4,3,6]) //[ 1, 11, 6, 5, 3, 4 ] [ 4, 6, 1, 3, 11, 5 ]
const c5 = _.shuffle([['fred', 30], ['barney', 'a'],['some',1],['a','c']])
//[ [ 'some', 1 ], [ 'a', 'c' ], [ 'barney', 'a' ], [ 'fred', 30 ] ]
const c6 = _.shuffle([{ 'user': 'fred'},{'age': 40 },{ 'user': 'pebbles'}])
//[ { user: 'pebbles' }, { age: 40 }, { user:'fred' } ]
console.log(c6);
sample
const ccc = _.sample(["lisong", "heyan",'A','C'], 1);
console.log(ccc); //heyan
map
let ownerArr = [
{
"owner": "Colin",
"pets": [{"name":"dog1"}, {"name": "dog2"}]
},
{
"owner": "John",
"pets": [{"name":"dog3"}, {"name": "dog4"}]
}];
console.log(_.map(ownerArr, 'pets[1].name')); // 循环找寻深度数组的值 [ 'dog2', 'dog4' ]
console.log(_.map(ownerArr,o=>o.pets[1].name)) // [ 'dog2', 'dog4' ]
const users = [
{ 'user': 'barney' },
{ 'user': 'fred' }
];
const c7 = _.map(users, 'user'); //[ 'barney', 'fred' ]
const c8 = _.map(users,o=>o.user) //[ 'barney', 'fred' ]
uniqueId
const c9= _.uniqueId('userId') //userId1
const c10 = _.uniqueId('userId') //userId2
const c11 = _.uniqueId('userId') //userId3
-
find
、filter
let F = [
{id: 0, name: "aaa", age: 33},
{id: 1, name: "bbb", age: 25},
{id: 2, name: "ccc", age: 25}
];
const sss = _.find(F, ["id", _.max(_.map(F, "id"))]); //json字符串中某一项最大的
// console.log(sss); // { id: 2, name: 'ccc', age: 25 }
const ss1 = _.find(F, ["id", 2]) // { id: 2, name: 'ccc', age: 25 }
// console.log(ss1);
const ss2 = _.filter(F, ["id", 2]) // [ { id: 2, name: 'ccc', age: 25 } ] 返回一个新的过滤后的数组。
const ss3 = _.filter(F, ["id", _.max(_.map(F, "id"))]) // [ { id: 2, name: 'ccc', age: 25 } ]
const d11 = Array.prototype.slice.apply(["a","b"]);
console.log(d11);
// Array.prototype.slice.call(arguments)
const d13 = new Array(3)
console.log(d13);
compact
_.compact([0, 1, false, 2, '', 3]);
// => [1, 2, 3]
-
_.groupBy()
image
将上图转换成下图这种数据格式:
image
const courseGroup = _.groupBy(arr, 'lesson.courseSession.objectId')
或
const courseGroup = _.groupBy(arr, e => {
return e.lesson.courseSession.objectId
)
image
Object.values()
const finalData = Object.values(courseGroup)