一、_.assign(object, [sources]):对象的合并继承(不包含原型),后面覆盖前面的。数组
_.assign({a:1},{a:2},{a:3}) //{a: 3}
相似方法:
_.assignIn(object, [sources]):同样是合并继承,它就连原型都一并处理了。其实就是原来的_.extend()方法。
高级方法:
_.assignWith(object, sources, [customizer]):略。
_.assignInWith(object, sources, [customizer]):略。ide
二、_.at(object, [paths]):取出object指定位置的值并组成一个数组。函数
var object = { 'a': [{ 'b': { 'c': 3 } }, 4] }; _.at(object, ['a[0].b.c', 'a[1]']); // => [3, 4]
三、_.create(prototype, [properties]):常见一个从原型继承的对象,并能够添加本身的属性。通常用于原型的继承。this
function Shape() { this.x = 0; this.y = 0; } function Circle() { } Circle.prototype = _.create(Shape.prototype, { 'constructor': Circle }); var circle = new Circle; circle instanceof Circle; // => true circle instanceof Shape; // => true
四、_.defaults(object, [sources]):一样是继承合并对象,若是属性名同样,只保留最初的值。
相似方法:
_.defaultsDeep(object, [sources]):深合并,遇到嵌套的对象也会逐级对比合并。prototype
_.defaults({ a: {b:2} }, {a: {b:1,c:3} }); // { a:{b: 2} } _.defaultsDeep({ a: {b:2} }, {a: {b:1,c:3} }); // { a:{b: 2, c: 3} }
五、_.findKey(object, [predicate=_.identity]):经过value查找key,仅匹配第一个查询到的项。code
var users = { 'barney': { 'age': 36, 'active': true }, 'fred': { 'age': 40, 'active': false }, 'pebbles': { 'age': 1, 'active': true } }; _.findKey(users, 'active'); // => 'barney'
相似方法:
_.findLastKey(object, [predicate=_.identity]):从后往前匹配。对象
六、_.forIn(object, [iteratee=_.identity]):对象的for in 遍历,包括原型上的属性。
_.forInRight(object, [iteratee=_.identity]):反向遍历。
_.forOwn(object, [iteratee=_.identity]):仅遍历自身属性,不包括原型。
_.forOwnRight(object, [iteratee=_.identity]):反向遍历自身属性。继承
function Foo() { this.a = 1; this.b = 2; } Foo.prototype.c = 3; _.forIn(new Foo, (value, key)=> console.log(key)); // a b c _.forInRight(new Foo, (value, key)=> console.log(key)); // c b a _.forOwn(new Foo, (value, key)=> console.log(key)); // a b _.forOwnRight(new Foo, (value, key)=> console.log(key)); // b a
七、_.functions(object):返回对象下可枚举的方法名称组成的数组,经过原型继承的不算。
_.functionsIn(object):同上,经过原型继承的也算。ci
function Foo() { this.a = function(){}; this.b = function(){}; } Foo.prototype.c = function(){}; _.functions(new Foo); // => ['a', 'b'] _.functionsIn(new Foo); // => ['a', 'b', 'c']
八、_.get(object, path, [defaultValue]):获取objdect指定路径的值,若是undefined就返回defalutValue。这个功能感受有点鸡肋。get
九、_.has(object, path):检查 path 是不是object对象的直接属性。
_.**hasIn**(object, path):
var object = { 'a': { 'b': 2 } }; var other = _.create({ 'a': _.create({ 'b': 2 }) }); _.has(object, 'a.b'); // => true _.has(other, 'a'); // => false _.hasIn(other, 'a'); // => true
十、_.invert(object):这个挺有趣的,把object的key和value对调。
_.**invertBy**(object, [iteratee=_.identity]):自定义迭代函数。
var object = { 'a': 1, 'b': 2, 'c': 1 }; _.invert(object); // => { '1': 'c', '2': 'b' } _.invertBy(object); // => { '1': ["a", "c"], '2': ['b' ]} _.invertBy(object, value => 'group' + value); //=> { 'group1': ["a", "c"], 'group2': ['b' ]}