lodash用法系列2: 处理对象

Lodash用来操作对象和集合,比Underscore拥有更多的功能和更好的性能。

官网:https://lodash.com/
引用:
安装:npm install lodash

首先通过npm安装lodash:
npm i --save lodash

在js文件中引用lodash:
var _ = require('lodash');  

import * as _ from "lodash";

 目录:

  1. 判断参数是否是对象 isPlainObject
  2. 对象添加字段,相同字段值重写 assign
  3. 对象添加字段,保持原来字段的值 defaults
  4. 对象合并 merge
  5. 对象:根据值,寻找键 findKey
  6. 对象查询 find where
  7. 对象查询:根据多个属性值,获取对象其他属性值 result findWhere
  8. 遍历对象 forOwn
  9. 遍历对象,对象有父对象 forIn
  10. 获取对象的所有字段 keys
  11. 获取对象所有值 values at
  12. 获取对象中的某些字段 pick
  13. 删除对象中的某些字段 omit
  14. 对象的键值对转换成数组元素 pairs
  15. 颠倒对象的键和值 invert
  16. 拷贝对象 clone
  17. 获取一个对象的所有方法名 functions

 

1. 判断参数是否是对象 isPlainObject(obj)

function hello(greeting, person){
    if(_.isPlainObject(greeting)){
        person = greeting;
        greeting = 'hi';
    }
    return greeting + person.name;
}

hello('hello',{name:''});
hello({name:''});

2. 对象添加字段,相同字段值重写  assign

var o = {
    name: '',
    age:22
};

_.assign(o, {occupation:''});

3. 对象添加字段,保持原来字段的值 defaults

var o = {
    name: 'a'
};

_.defaults(o, {
    name: 'b'
});

4. 对象合并 merge

var o1 = {
    states: {running: 'off'},
    names: ['a','b']
};

var o2 = {
    states: {off: 'on'},
    names: ['c','d']
};

var result = _.merge(o1, o2, function(dest, src){
    if(_.isArray(dest) && _.isArray(src)){
        return dest.concat(src);
    }
});

//{ states: { running: 'off', off: 'on' },names: [ 'a', 'b', 'c', 'd' ] }
console.log(result);

以上,o1和o2都有相同的字段,使用merge方法后会遍历o1与o2的所有字段,但可喜的是:如果o2和o1的字段相同,并没有进行简单的重写字段值,而是进行了合并。并且,还可以判断o1和o2的字段类型,如果是数组,就把两个数组concat。

5. 对象:根据值,寻找键 findKey

1. findkey方法接受对象

var obj = {
    a: {
        name: 'a',
        age:20
    },
    b: {
        description:''
    }
};

var result = _.findKey(obj, {name: 'a'});

//a
console.log(result);

2. findKey 方法接受匿名函数

var o = {
    name: 'a',
    age: 20
}

var result = _.findKey(o, function(value){
    return value === 'a';
});

//name
console.log(result);


// eg2:
var obj = {
    a: ['x','y'],
    b: ['m','n']
};

var search = 'x';

var result = _.findKey(obj, function(value){
    if(_.isArray(value)){
        return _.contains(value, search);
    } else {
        return value === search;
    }
});

//a
console.log(result);

6. 对象查询 find where

var o = {
    1: {
        first:'',
        enabled:false
    },
    2: {
        first: '',
        enabled:true
    }
};

var result = _.find(o, 'enabled');
var result = _.where(o, {first: ''});

 7. 对象查询:根据多个属性值,获取对象其他属性值 result  findWhere

ar users = [
  { ‘user‘: ‘barney‘, ‘age‘: 36, ‘active‘: true },
  { ‘user‘: ‘fred‘,   ‘age‘: 40, ‘active‘: false }
];

_.result(_.findWhere(users, { ‘age‘: 36, ‘active‘: true }), ‘user‘);  // => ‘barney‘

_.result(_.findWhere(users, { ‘age‘: 40, ‘active‘: false }), ‘user‘);  // => ‘fred‘

8. 遍历对象 forOwn

var o = {
    name: '',
    age:20,
    description:''
};

var result = [];

_.forOwn(o, function(value, key){
    result.push(key + ': ' + value);
});

console.log(result);

可见,forOwn方法forEach方法的不同之处在于匿名函数的第二个参数是键,不是索引

9. 遍历对象,对象有父对象  forIn

unction Person(){
    this.full = function(){return this.first + ' ' + this.last;};
}

function Employee(first, last, occupation){
    this.first = first;
    this.last = last;
    this.occupation = occupation;
}

Employee.prototype = new Person();

var employee = new Employee('darren','ji','programmer'),
    resultOwn = [],
    resultIn = [];

_.forOwn(employee, function(value, key){
   resultOwn.push(key);
});

//[ 'first', 'last', 'occupation' ]
console.log(resultOwn);

_.forIn(employee, function(value, key){
   resultIn.push(key);
});

//[ 'first', 'last', 'occupation', 'full' ]
console.log(resultIn);

可见,forIn会遍历包括父对象的字段,forOwn只遍历当前对象的字段

10. 获取对象的所有字段 keys

var o1 = {
    occupation: '',
    last:'',
    first:''
};
var o1Keys = _.keys(o1);   // 获取所有键

var result1 = _.sortBy(o1Keys);  //所有键排序

//[ 'first', 'last', 'occupation' ]
console.log(result1);

11. 获取对象所有值 values at

1. 直接获取所有值

var o = {};
_.values(o);

2. 获取对象的所有key,然后据key或取值  at

var o2 = {
    occupation: 'manager',
    last: 'ji',
    first: 'darren'
};

//[ 'darren', 'ji', 'manager' ]
console.log(_.at(o2, _.sortBy(_.keys(o2))));

12. 获取对象中的某些字段  pick

var o1 = {
    name: 'a',
    occupation:'b'
},

console.log(_.pick(o2, 'spcecialty'));  //{ spcecialty: 'c' }

13. 删除对象中的某些字段 omit

// 去除值为 bool,o, null 的字段
var o = {
    name: 'a',
    age:0,
    occupation:null,
    enabled: true
};

var r = _.omit(o, function(value){
    return !(!_.isBoolean(value) && value);
});

console.log(r);  //{ name: 'a' }

1 4. 对象的键值对转换成数组元素 pairs

var o = {
    first: 'darren',
    last: 'ji',
    age:33
};

console.log(_.pairs(o)); //[ [ 'first', 'darren' ], [ 'last', 'ji' ], [ 'age', 33 ] ]

15. 颠倒对象的键和值 invert

var o = {
    first: 'a',
    last: 'b'
}

console.log(_.invert(o));  //{ a: "first", b: "last"}

16. 拷贝对象  clone

var o = {
    first: 'darren',
    last: 'ji'
},
clone = _.clone(o),

console.log(clone.first);  //darren

17. 获取一个对象的所有方法名 functions

function Person(first, last){
    this.first = first;
    this.last = last;
}

Person.prototype.name = function(){
    return this.first + ' ' + this.last;
}

//[ 'name' ]
var result = _.functions(new Person('darren','ji'));

 

参考: https://www.cnblogs.com/darrenji/p/5011370.html

你可能感兴趣的:(lodash用法系列2: 处理对象)