lodash常用函数解释

来源:互联网 发布:淘宝申请退货在哪里 编辑:程序博客网 时间:2024/06/11 05:19

_.assign(object, [sources], [customizer], [thisArg])

作用:分配源对象可列举的属性给目标对象,后来分配的源会覆盖先前的源
object :目标对象
sources :可选,源对象
customizer :可选,自定义函数的值
thisArg :绑定的编辑器
返回值: 对象
例子:
_.assign({ 'user': 'barney' }, { 'age': 40 }, { 'user': 'fred' });// => { 'user': 'fred', 'age': 40 }


_.get(object, path, [defaultValue])

作用:获取对象给定路径的属性值,如果获取不到值,将返回默认值
object : 要查询的对象
path : 要获取的属性的路径
defaultValue : 默认值
例子:
var object = { 'a': [{ 'b': { 'c': 3 } }] };_.get(object, 'a[0].b.c');// => 3_.get(object, ['a', '0', 'b', 'c']);// => 3_.get(object, 'a.b.c', 'default');// => 'default'



_.includes(collection, target, [fromIndex=0])

作用:检查collection中是否含有target
collection : 数组,对象或者字符串
target : 要搜索的值
fromIindex : 开始搜索的下标,如果下标为负,那其表示距离end节点的距离
例子:
_.includes([1, 2, 3], 1);// => true_.includes([1, 2, 3], 1, 2);// => false_.includes({ 'user': 'fred', 'age': 40 }, 'fred');// => true_.includes('pebbles', 'eb');// => true


_.forOwn(object, [iteratee=_.identity], [thisArg])

作用:遍历对象自身可枚举的属性
object : 要遍历的对象
iteratee=_.identity : 被每一个迭代调用的函数,可选
thisArg : iteratee绑定的参数(暂不知道用处)
例子:
function Foo() {  this.a = 1;  this.b = 2;}Foo.prototype.c = 3;_.forOwn(new Foo, function(value, key) {  console.log(key);});// => logs 'a' and 'b' (iteration order is not guaranteed)


0 0
原创粉丝点击