1. Loadsh是什么:
let json = _.map(datas);
你会很疑问下划线“_”是什么,其实可以认为是Loadsh库的一个对象,这样就可以使用Loadsh库的各种便捷方法了。
官方文档:https://lodash.com/docs/4.17.11 墙裂推荐从这里面查!!
2_.join():字符串拼接
let hell = _.join(['Hello','webpack'], ' ');
将参数1中的各个元素,通过参数2连接起来。
3_.filter() 方法
创建一个新的数组,新数组中的元素是通过检查指定数组中符合条件的所有元素。
参数:
collection (Array|Object): The collection to iterate over.
[predicate=_.identity] (Function): The function invoked per iteration.(每次遍历都会调用的函数,函数返回true的才会被返回)
返回:
(Array): Returns the new filtered array.
示例:
var users = [
{ 'user': 'barney', 'age': 36, 'active': true },
{ 'user': 'fred', 'age': 40, 'active': false }
];
_.filter(users, function(o) { return !o.active; });
// => objects for ['fred']
//类似的几种写法:
// The `_.matches` iteratee shorthand.
_.filter(users, { 'age': 36, 'active': true });
// => objects for ['barney']
// The `_.matchesProperty` iteratee shorthand.
_.filter(users, ['active', false]);
// => objects for ['fred']
// The `_.property` iteratee shorthand.
_.filter(users, 'active');
// => objects for ['barney']
4_.slice()方法
切片方法:返回数组中指定起始位置 到 终止位置的元素,左闭右开
_.slice(array, [start=0], [end=array.length])
参数:
array (Array): The array to slice.
[start=0] (number): The start position.
[end=array.length] (number): The end position.
返回:
(Array): Returns the slice of array.
5_.orderBy() 方法:排序
var users = [
{ ‘user’: ‘fred’, ‘age’: 48 },
{ ‘user’: ‘barney’, ‘age’: 34 },
{ ‘user’: ‘fred’, ‘age’: 40 },
{ ‘user’: ‘barney’, ‘age’: 36 }
];
// Sort by user
in ascending order and by age
in descending order.
_.orderBy(users, [‘user’, ‘age’], [‘asc’, ‘desc’]);
// => objects for [[‘barney’, 36], [‘barney’, 34], [‘fred’, 48], [‘fred’, 40]]
参数:
1. collection (Array|Object): The collection to iterate over.
2. [iteratees=[_.identity]] (Array[]|Function[]|Object[]|string[]): The iteratees to sort by.
3. [orders] (string[]): The sort orders of iteratees.,有asc和desc两种
6 _.intersectionBy():取交集
找出多个数组之间相交的元素(也就是都拥有的元素,等同于离散数学中的交集)
7_.clone,_cloneDeep() 的区别是前者是浅克隆,后者是深克隆(很少用到)
var objects = [{ ‘a’: 1 }, { ‘b’: 2 }];
var shallow = _.clone(objects);
console.log(shallow[0] === objects[0]);
// => true
var objects = [{ ‘a’: 1 }, { ‘b’: 2 }];
var deep = _.cloneDeep(objects);
console.log(deep[0] === objects[0]);
// => false