lodash常用API

• 这个函数把数组按照一定的长度分开，返回新的数组。（片段化数组）
```const arr = [1,2,3,4,5,6,7,8,9];
_.chunk(arr,2);
// =>[[1,2],[3,4],[5,6],[7,8],[9]]
```

compact

```_.compact(['1','2',' ',0])
// => ['1','2']
```

uniq

```_.uniq([1,1,3])
// => [1,3]
```

_.reject

```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

```_.shuffle([1,2,3,4]);// 无序化
```

_.pick

```var foo = {id: 0, name: "aaa", age: 33}
var bar = _.pick(foo, ['name', 'age'])
//bar = {name: "aaa", age: 33}
```

_.keys

```var foo = {id: 0, name: "aaa", age: 33}
var bar = _.keys(foo)
//bar = ['id', 'name', 'age']
```

_.forEach

常规数组遍历

```agent.forEach(function (n,key) {
agent[key].agent_id = agent[key].agent_name;
return agent;
})
```

使用lodash来遍历

```_.forEach(agent,function(n,key) {

agent[key].agent_id= agent[key].agent_name

})
```

```const arr = _.forEach();
```

_.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 }] }
```

_.random

```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}
```

_.keyBy

```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}
//}
```

_.filter

```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}
//}
```

_.map

```var foo = var foo = [
{id: 0, name: "aaa", age: 33},
{id: 1, name: "bbb", age: 25}
]
var bar = _.map(foo, 'name')
//bar = ["aaa", "bbb"]
```

.max/ .min/_.sum

```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 = ----helloworld
//bar = helloworld----
```

选出json数组中id最大的一项

```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来代替
```

更新json数组中某一项的值

```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}
//]
```

扁平化数组 _.flatten

```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 } ]
```