lodash速览:对象Object要领(一)

1、_.assign(object, [sources]):对象的兼并继续(不包含原型),背面掩盖前面的。

_.assign({a:1},{a:2},{a:3})    //{a: 3}

相似要领:
_.assignIn(object, [sources]):一样是兼并继续,它就连原型都一并处理了。实在就是本来的_.extend()要领。
高等要领:
_.assignWith(object, sources, [customizer]):略。
_.assignInWith(object, sources, [customizer]):略。

2、_.at(object, [paths]):掏出object指定位置的值并构成一个数组。

var object = { 'a': [{ 'b': { 'c': 3 } }, 4] };
_.at(object, ['a[0].b.c', 'a[1]']);     // => [3, 4]

3、_.create(prototype, [properties]):罕见一个从原型继续的对象,并可以增加本身的属性。平常用于原型的继续。

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

4、_.defaults(object, [sources]):同样是继续兼并对象,假如属性名一样,只保存最初的值。
相似要领:
_.defaultsDeep(object, [sources]):深兼并,碰到嵌套的对象也会逐级对照兼并。

_.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} }

5、_.findKey(object, [predicate=_.identity]):经由过程value查找key,仅婚配第一个查询到的项。

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]):从后往前婚配。

6、_.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

7、_.functions(object):返回对象下可罗列的要领称号构成的数组,经由过程原型继续的不算。
_.functionsIn(object):同上,经由过程原型继续的也算。

function Foo() {
  this.a = function(){};
  this.b = function(){};
}
Foo.prototype.c = function(){};
_.functions(new Foo);        // => ['a', 'b']
_.functionsIn(new Foo);        // => ['a', 'b', 'c']

8、_.get(object, path, [defaultValue]):猎取objdect指定途径的值,假如undefined就返回defalutValue。这个功用觉得有点鸡肋。

9、_.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

10、_.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' ]}
    原文作者:梵天白莲
    原文地址: https://segmentfault.com/a/1190000015733808
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞