lodash常用方法

1._.get
说明: 其实就是如果没有这个值以后就会返回undefined,而不会像js中没有这个值就会报错

var users = {   'age': 40, 'active': false }
  
console.log( _.get(users, "user") );

2._.cloneDeep
说明:深度克隆

var objects = [{ 'a': 1 }, { 'b': 2 }];

var deep = _.cloneDeep(objects);
console.log(deep[0] === objects[0]);

3._.isEqual

说明:执行深比较来决定两者的值是否相等。

var object = { 'user': 'fred' };
var other = { 'user': 'fred' };

_.isEqual(object, other);
// => true

object === other;
// => false

4._.compact(array)
说明:创建一个移除了所有假值的数组。例如:false、null、 0、""、undefined, 以及NaN 都是 “假值”.

_.compact([0, 1, false, 2, '', 3]);
// => [1, 2, 3]

5._.truncate([string=''], [options])
说明:截断字符串,如果字符串超出了限定的最大值。 被截断的字符串后面会以 omission 代替,omission 默认是 "..."

_.truncate('hi-diddly-ho there, neighborino');
// => 'hi-diddly-ho there, neighbo...'

_.truncate('hi-diddly-ho there, neighborino', {
  'length': 24,
  'separator': ' '
});
// => 'hi-diddly-ho there,...'

_.truncate('hi-diddly-ho there, neighborino', {
  'length': 24,
  'separator': /,? +/
});
// => 'hi-diddly-ho there...'

_.truncate('hi-diddly-ho there, neighborino', {
  'omission': ' [...]'
});
// => 'hi-diddly-ho there, neig [...]'
  1. .keyBy(collection, [iteratee=.identity])
    说明:创建一个对象组成。key 是经 iteratee 处理的结果,value 是产生key的元素。 iteratee 会传入1个参数:(value)。
_.keyBy(array, 'dir');
// => { 'left': { 'dir': 'left', 'code': 97 }, 'right': { 'dir': 'right', 'code': 100 } }

7._.uniq(array)

说明:返回不重复的数组。

_.uniq([2, 1, 2]);
// => [2, 1]

8..findIndex(array, [predicate=.identity])

说明:返回符合元素的 index,否则返回 -1。

var users = [
  { 'user': 'barney',  'active': false },
  { 'user': 'fred',    'active': false },
  { 'user': 'pebbles', 'active': true }
];

_.findIndex(users, function(o) { return o.user == 'barney'; });
// => 0

9._.values(object)
把对象的值转化为数组
相当于es6的Object.values

function Foo() {
  this.a = 1;
  this.b = 2;
}

_.values(new Foo);
// => [1, 2] (无法保证遍历的顺序)

10._.keys()
把对象的属性转化为数组
相当于es6的Object.keys

function Foo() {
  this.a = 1;
  this.b = 2;
}

_.keys(new Foo);
// => ['a', 'b'] (无法保证遍历的顺序)

11._.forIn()
*相当于es6的Object.enteries

_.forIn(users,(value,key)=>{
  console.log(value,key );
})


12._.isEmpty判断是否为空

console.log(_.isEmpty({}));
输出true

13._.unionBy数组对象去重

第二个参数为根据什么来去重
_.unionBy([{ 'x': 1 }], [{ 'x': 2 }, { 'x': 1 }], 'x');
// => [{ 'x': 1 }, { 'x': 2 }]复制代码

14._.debounce函数防抖

第二个参数是等待时间,如果触发函数间隔的时间大于这个数,那么才行第一个参数
_.debounce(this.fetchUser, 200)

你可能感兴趣的:(lodash常用方法)