本文主要是介绍lodash几个常用方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
_.cloneDeep(value)
var objects = [{ 'a': 1 }, { 'b': 2 }];var deep = _.cloneDeep(objects);
console.log(deep[0] === objects[0]);
// => false
_.debounce
按钮防抖
created() {this.save = _.debounce(this.save, 300)
},
_.omit(object, [props])
对象去除
var object = { 'a': 1, 'b': '2', 'c': 3 };_.omit(object, ['a', 'c']);
// => { 'b': '2' }
_.pull(array, [values])
数组去除
var array = [1, 2, 3, 1, 2, 3];_.pull(array, 2, 3);
console.log(array);
// => [1, 1]
组合用法
this.options.series[0].type = _.sample(_.pull(['line', 'bar', 'scatter'], [this.options.series[0].type]))
_.keys(object)
取出对象中所有的key值组成新的数组
function Foo() {this.a = 1;this.b = 2;
}Foo.prototype.c = 3;_.keys(new Foo);
// => ['a', 'b'] (iteration order is not guaranteed)_.keys('hi');
// => ['0', '1']
_.pick(object, [props])
var object = { 'a': 1, 'b': '2', 'c': 3 };_.pick(object, ['a', 'c']);
// => { 'a': 1, 'c': 3 }
组合用法
this.form = _.pick(row, _.keys(this.form))
_.sample(collection)
随机
_.sample([1, 2, 3, 4]);
// => 2
_.random([lower=0], [upper=1], [floating])
随机,下限上限,返回是否有浮点数
_.random(0, 5);
// => an integer between 0 and 5_.random(5);
// => also an integer between 0 and 5_.random(5, true);
// => a floating-point number between 0 and 5_.random(1.2, 5.2);
// => a floating-point number between 1.2 and 5.2
_.round(number, [precision=0])
数字,精度
_.round(4.006);
// => 4_.round(4.006, 2);
// => 4.01_.round(4060, -2);
// => 4100
_.assign(object, [sources])
对象拼接
function Foo() {this.a = 1;
}function Bar() {this.c = 3;
}Foo.prototype.b = 2;
Bar.prototype.d = 4;_.assign({ 'a': 0 }, new Foo, new Bar);
// => { 'a': 1, 'c': 3 }
这篇关于lodash几个常用方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!