// 常用的数组方法 按长度进行切割数组
const arr = [1,2,3,4]
const newArr = _.chunk(arr,2)
console.log(newArr);
// 过滤假值
const arr = [1,false,undefined,NaN,'',22,null]
const newArr = _.compact(arr)
或者
const newArr = arr.filter(Boolean)
console.log(newArr);
// 数组去重 (将数组中的对象去重,只能是数组去重,不能是对象去重。)
const arr = [1,false,undefined,NaN,'',22,null,1]
const newArr = _.uniq(arr)
console.log(newArr);
// 根据条件去除某个元素
const arr = [
{id: 0, name: "aaa", age: 33},
{id: 1, name: "bbb", age: 25},
]
const newArr = _.reject(arr,["id",0])
// 打乱顺序
_.shuffle([1,2,3,4]);// 无序化
// 常用的对象方法 过滤需要的属性 pick
const arr = {id:0,name:'22',age: '22'}
const newArr = _.pick(arr,['name','age'])
console.log(newArr);
// 返回object中的所有key
const obj = {id: 0, name: "aaa", age: 33};
const newArr = _.keys(obj)
console.log(newArr);
// 深度拷贝
const obj = {id: 0, name: "aaa", age: 33,obj:{id: 1, name:
"aaa",obj:{ obj:{id: 2}}}};
const newArr = _.cloneDeep(obj)
console.log(newArr);
// _random
console.log(_.random(10, 20)); // 获取随机数
_.random(15, 20, true); // 随机浮点
_.sample(["lisong", "heyan"], 1);// 随机获取数组中的某一项,attr2:随机获取的个数
数组去重
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 } ]
转载http://blog.poetries.top/2018/12/06/lodash-api/