Module: object
对象相关函数
Functions
assign
▸ assign(target
, ...sources
): Record
<UnknownMapKey
, any
>
将一个或多个源对象的可枚举属性值分配到目标对象。如果源对象有多个,则按照从左到右的顺序依次对target赋值,相同属性会被覆盖
该函数会修改目标对象
- 当目标对象是null/undefined时,返回空对象
- 当目标对象是基本类型时,返回对应的包装对象
- 当目标对象是不可扩展/冻结/封闭状态时,返回目标对象
Example
//{x:1,y:3}console.log(_.assign({x:1},{y:3}))
Parameters
Name | Type | Description |
---|---|---|
target | Record <UnknownMapKey , any > | 目标对象 |
...sources | any | 源对象 |
Returns
Record
<UnknownMapKey
, any
>
返回target
assignWith
▸ assignWith(target
, ...sources
): Record
<UnknownMapKey
, any
>
与assign
相同,但支持自定义处理器
该函数会修改目标对象
Example
//{x: 1, y: '3y', z: null}console.log(_.assignWith({x:1},{y:3,z:4},(sv,tv,k)=>k=='z'?null:sv+k))
Parameters
Name | Type | Description |
---|---|---|
target | Record <UnknownMapKey , any > | 目标对象 |
...sources | any | 源对象 |
Returns
Record
<UnknownMapKey
, any
>
返回target
clone
▸ clone(obj
): string
| boolean
| Record
<UnknownMapKey
, any
>
浅层复制对象 如果是基本类型,返回原值 如果是函数类型,返回原值 只复制对象的自身可枚举属性
Example
//nullconsole.log(_.clone(null))
Parameters
Name | Type |
---|---|
obj | Record <UnknownMapKey , any > |
Returns
string
| boolean
| Record
<UnknownMapKey
, any
>
被复制的新对象
cloneDeep
▸ cloneDeep(obj
): Record
<UnknownMapKey
, any
>
完整复制对象,可以保持被复制属性的原有类型
如果obj是基本类型,返回原值 如果obj是函数类型,返回原值 只复制对象的自身可枚举属性
Example
//trueconsole.log(_.cloneDeep({d:new Date}).d instanceof Date)
Parameters
Name | Type |
---|---|
obj | Record <UnknownMapKey , any > |
Returns
Record
<UnknownMapKey
, any
>
被复制的新对象
cloneDeepWith
▸ cloneDeepWith(obj
, handler?
): any
完整复制对象,可以保持被复制属性的原有类型。支持赋值处理器
如果obj是基本类型,返回原值 如果obj是函数类型,返回原值 只复制对象的自身可枚举属性
Example
//trueconsole.log(_.cloneDeepWith({d:new Date}).d instanceof Date)
Parameters
Name | Type | Description |
---|---|---|
obj | Record <UnknownMapKey , any > | |
handler? | (v : any , k : UnknownMapKey , obj : Record <UnknownMapKey , any >) => any | (objk,k,obj) 自定义赋值处理器,返回赋予新对象k的值。默认 identity |
Returns
any
被复制的新对象
cloneWith
▸ cloneWith(obj
, handler?
): string
| boolean
| Record
<UnknownMapKey
, any
>
浅层复制对象,支持赋值处理器 如果obj是基本类型,返回原值 如果obj是函数类型,返回原值
只复制对象的自身可枚举属性
Example
//{x: 1, y: 2, z: null}console.log(_.cloneWith({x:1,y:2,z:3},(v,k)=>k=='z'?null:v))//nullconsole.log(_.cloneWith(null))
Parameters
Name | Type | Default value | Description |
---|---|---|---|
obj | Record <UnknownMapKey , any > | undefined | |
handler? | (v : any , k : string | number | symbol ) => any | identity | (objk,k) 自定义赋值处理器,返回赋予新对象k的值 |
Returns
string
| boolean
| Record
<UnknownMapKey
, any
>
被复制的新对象
defaults
▸ defaults<T
>(target
, ...sources
): T
将一个或多个源对象的可枚举属性值分配到目标对象中属性值为undefined的属性上。 如果源对象有多个,则按照从左到右的顺序依次对target赋值,相同属性会被忽略
该函数会修改目标对象
- 当目标对象是null/undefined时,返回空对象
- 当目标对象是基本类型时,返回对应的包装对象
- 当目标对象是不可扩展/冻结/封闭状态时,返回目标对象
Example
//{a: 1, b: 2, c: 3}console.log(_.defaults({a:1},{b:2},{c:3,b:1,a:2}))
Since
0.21.0
Type parameters
Name | Type |
---|---|
T | extends Record <UnknownMapKey , any > |
Parameters
Name | Type | Description |
---|---|---|
target | T | 目标对象 |
...sources | any | 1-n个源对象 |
Returns
T
返回target
defaultsDeep
▸ defaultsDeep<T
>(target
, ...sources
): T
与defaults
相同,但会递归对象属性
该函数会修改目标对象
Example
//{a: {x: 1, y: 2, z: 3}, b: 2}console.log(_.defaultsDeep({a:{x:1}},{b:2},{a:{x:3,y:2}},{a:{z:3,x:4}}))
Since
0.21.0
Type parameters
Name | Type |
---|---|
T | extends Record <UnknownMapKey , any > |
Parameters
Name | Type | Description |
---|---|---|
target | T | 目标对象 |
...sources | any | 1-n个源对象 |
Returns
T
返回target
findKey
▸ findKey<V
>(object
, predicate
): string
| number
| symbol
| undefined
对object
内的所有属性进行断言并返回第一个匹配的属性key
Example
const libs = { 'func.js':{platform:['web','nodejs'],tags:{utils:true}}, 'juth2':{platform:['web','java'],tags:{utils:false,middleware:true}}, 'soya2d':{platform:['web'],tags:{utils:true}}}//func.js 查询对象的keyconsole.log(_.findKey(libs,'tags.utils'))//juth2console.log(_.findKey(libs,{'tags.utils':false}))//tagsconsole.log(_.findKey(libs['soya2d'],'utils'))//2console.log(_.findKey([{a:1,b:2},{c:2},{d:3}],'d'))
Type parameters
Name |
---|
V |
Parameters
Name | Type | Description |
---|---|---|
object | Collection <unknown > | 所有集合对象array / arrayLike / map / object / ... |
predicate | NonFuncItee | (value : V , index : UnknownMapKey , collection : Collection <unknown >) => boolean | (value,index|key,collection) 断言 当断言是函数时回调参数见定义 其他类型请参考 iteratee |
Returns
string
| number
| symbol
| undefined
第一个匹配断言的元素的key或undefined
fromPairs
▸ fromPairs(pairs
): Record
<UnknownMapKey
, any
>
toPairs
反函数,创建一个由键值对数组组成的对象
Example
//{a:1,b:2,c:3}console.log(_.fromPairs([['a', 1], ['b', 2], ['c', 3]]))
Parameters
Name | Type | Description |
---|---|---|
pairs | any [] | 键值对数组 |
Returns
Record
<UnknownMapKey
, any
>
对象
functions
▸ functions(obj
): string
返回对象中的函数属性key数组
Example
const funcs = { a(){}, b(){}};//[a,b]console.log(_.functions(funcs))//[....]console.log(_.functions(_))
Since
0.18.0
Parameters
Name | Type |
---|---|
obj | Record <UnknownMapKey , any > |
Returns
string
函数名数组
get
▸ get<V
>(obj
, path
, defaultValue?
): V
通过path获取对象属性值
Example
//2console.log(_.get([1,2,3],1))//Holyhighconsole.log(_.get({a:{b:[{x:'Holyhigh'}]}},['a','b',0,'x']))//Holyhigh2console.log(_.get({a:{b:[{x:'Holyhigh2'}]}},'a.b.0.x'))//Holyhighconsole.log(_.get({a:{b:[{x:'Holyhigh'}]}},'a.b[0].x'))//hiconsole.log(_.get([[null,[null,null,'hi']]],'[0][1][2]'))//not findconsole.log(_.get({},'a.b[0].x','not find'))
Type parameters
Name |
---|
V |
Parameters
Name | Type | Description |
---|---|---|
obj | Record <UnknownMapKey , any > | 需要获取属性值的对象,如果obj不是对象(isObject返回false),则返回defaultValue |
path | string | number | (string | number ) | 属性路径,可以是索引数字,字符串key,或者多级属性数组 |
defaultValue? | any | 如果path未定义,返回默认值 |
Returns
V
属性值或默认值
has
▸ has(obj
, key
): boolean
检查指定key是否存在于指定的obj中
Example
//trueconsole.log(_.has({a:12},'a'))
Parameters
Name | Type |
---|---|
obj | Record <UnknownMapKey , any > |
key | UnknownMapKey |
Returns
boolean
如果key存在返回true
keys
▸ keys(obj
): string
返回对象的所有key数组
Example
let f = new Function("this.a=1;this.b=2;");f.prototype.c = 3;//[a,b]console.log(_.keys(new f()))
Parameters
Name | Type |
---|---|
obj | Record <UnknownMapKey , any > |
Returns
string
对象的key
keysIn
▸ keysIn(obj
): string
返回对象的所有key数组 包括原型链中的属性key
Example
let f = new Function("this.a=1;this.b=2;");f.prototype.c = 3;//[a,b,c]console.log(_.keysIn(new f()))
Parameters
Name | Type |
---|---|
obj | Record <UnknownMapKey , any > |
Returns
string
对象的key
merge
▸ merge<T
>(target
, ...sources
): T
类似assign
,但会递归源对象的属性合并到目标对象。
如果目标对象属性值存在,但对应源对象的属性值为undefined,跳过合并操作。
支持自定义处理器,如果处理器返回值为undefined,启用默认合并。
该函数在对可选配置项与默认配置项进行合并时非常有用
该函数会修改目标对象
- 当目标对象是null/undefined时,返回空对象
- 当目标对象是基本类型时,返回对应的包装对象
- 当目标对象是不可扩展/冻结/封闭状态时,返回目标对象
Example
//{x: 0, y: {a: 1, b: 2, c: 3, d: 4}}console.log(_.merge({x:1,y:{a:1,b:2}},{x:2,y:{c:5,d:4}},{x:0,y:{c:3}}))//[{x: 0, y: {a: 1, b: 2, c: 3, d: 4}}]console.log(_.merge([{x:1,y:{a:1,b:2}}],[{x:2,y:{c:5,d:4}}],[{x:0,y:{c:3}}]))
Since
0.22.0
Type parameters
Name | Type |
---|---|
T | extends Record <UnknownMapKey , any > |
Parameters
Name | Type | Description |
---|---|---|
target | T | 目标对象 |
...sources | any | 1-n个源对象 |
Returns
T
返回target
mergeWith
▸ mergeWith<T
>(target
, ...sources
): T
与merge
相同,但支持自定义处理器
该函数会修改目标对象
Example
//{x: 2, y: {a: 2, b: 4, c: 3, d: 27}}console.log(_.mergeWith({x:1,y:{a:1,b:2,c:3}},{x:2,y:{a:2,d:3}},{y:{b:4}},(sv,tv,k)=>k=='d'?sv*9:undefined))
Since
0.22.0
Type parameters
Name | Type |
---|---|
T | extends Record <UnknownMapKey , any > |
Parameters
Name | Type | Description |
---|---|---|
target | T | 目标对象 |
...sources | any | 1-n个源对象 |
Returns
T
返回target
omit
▸ omit(obj
, ...props
): Record
<UnknownMapKey
, any
>
创建一个剔除指定属性的对象子集并返回。与pick()刚好相反
Example
//{a: 1, c: '3'}console.log(_.omit({a:1,b:2,c:'3'},'b'))//{a: 1}console.log(_.omit({a:1,b:2,c:'3'},'b','c'))//{c: '3'}console.log(_.omit({a:1,b:2,c:'3'},['b','a']))
Since
0.16.0
Parameters
Name | Type | Description |
---|---|---|
obj | Record <UnknownMapKey , any > | 选取对象 |
...props | (string | string ) | 属性集合 |
Returns
Record
<UnknownMapKey
, any
>
对象子集
omitBy
▸ omitBy<V
, K
>(obj
, predicate?
): Record
<UnknownMapKey
, any
>
同omit
,但支持断言函数进行剔除
Example
//{c: '3'}console.log(_.omitBy({a:1,b:2,c:'3'},_.isNumber))
Since
0.23.0
Type parameters
Name | Type |
---|---|
V | V |
K | extends UnknownMapKey |
Parameters
Name | Type | Description |
---|---|---|
obj | Record <UnknownMapKey , any > | 选取对象 |
predicate? | (v : V , k : K ) => boolean | (v,k)断言函数 |
Returns
Record
<UnknownMapKey
, any
>
对象子集
pick
▸ pick(obj
, ...props
): Record
<UnknownMapKey
, any
>
创建一个指定属性的对象子集并返回
Example
//{b: 2}console.log(_.pick({a:1,b:2,c:'3'},'b'))//{b: 2,c:'3'}console.log(_.pick({a:1,b:2,c:'3'},'b','c'))//{a: 1, b: 2}console.log(_.pick({a:1,b:2,c:'3'},['b','a']))
Since
0.16.0
Parameters
Name | Type | Description |
---|---|---|
obj | Record <UnknownMapKey , any > | 选取对象 |
...props | (string | string ) | 属性集合 |
Returns
Record
<UnknownMapKey
, any
>
对象子集
pickBy
▸ pickBy(obj
, predicate?
): Record
<UnknownMapKey
, any
>
同pick
,但支持断言函数进行选取
Example
//{a: 1, b: 2}console.log(_.pickBy({a:1,b:2,c:'3'},_.isNumber))
Since
0.23.0
Parameters
Name | Type | Description |
---|---|---|
obj | Record <UnknownMapKey , any > | 选取对象 |
predicate? | (v : unknown , k : UnknownMapKey ) => boolean | (v,k)断言函数 |
Returns
Record
<UnknownMapKey
, any
>
对象子集
prop
▸ prop(path
): (obj
: Record
<UnknownMapKey
, any
>) => any
创建一个函数,该函数返回指定对象的path属性值
Example
const libs = [ {name:'func.js',platform:['web','nodejs'],tags:{utils:true},js:false}, {name:'juth2',platform:['web','java'],tags:{utils:false,middleware:true},js:true}, {name:'soya2d',platform:['web'],tags:{utils:true},js:true}];//[true,false,true]console.log(_.map(libs,_.prop('tags.utils')))//nodejsconsole.log(_.prop(['platform',1])(libs[0]))
Since
0.17.0
Parameters
Name | Type |
---|---|
path | string | string |
Returns
fn
接收一个对象作为参数的函数
▸ (obj
): any
Parameters
Name | Type |
---|---|
obj | Record <UnknownMapKey , any > |
Returns
any
set
▸ set(obj
, path
, value
): Record
<UnknownMapKey
, any
>
通过path设置对象属性值。如果路径不存在则创建,索引会创建数组,属性会创建对象
Example
//{"a":1,"b":{"c":[undefined,{"x":10}]}}console.log(_.set({a:1},'b.c.1.x',10))
Since
0.16.0
Parameters
Name | Type | Description |
---|---|---|
obj | Record <UnknownMapKey , any > | 需要设置属性值的对象,如果obj不是对象(isObject返回false),直接返回obj |
path | string | number | (string | number ) | 属性路径,可以是索引数字,字符串key,或者多级属性数组 |
value | any | 任何值 |
Returns
Record
<UnknownMapKey
, any
>
obj 修改后的源对象
toObject
▸ toObject(...vals
): Record
<UnknownMapKey
, any
>
解析传递参数并返回一个根据参数值创建的Object实例。 支持数组对、k/v对、对象、混合方式等创建 是 toPairs 的反函数
Example
//{a:1,b:2}console.log(_.toObject('a',1,'b',2))//如果参数没有成对匹配,最后一个属性值则为undefined//{a:1,b:2,c:undefined}console.log(_.toObject('a',1,'b',2,'c'))//{a:1,b:4,c:3} 重复属性会覆盖console.log(_.toObject(['a',1,'b',2],['c',3],['b',4]))//{a:1,b:2} 对象类型返回cloneconsole.log(_.toObject({a:1,b:2}))//{1:now time,a:{}} 混合方式console.log(_.toObject([1,new Date],'a',{}))
Parameters
Name | Type | Description |
---|---|---|
...vals | any | 对象创建参数,可以是一个数组/对象或者多个成对匹配的基本类型或者多个不定的数组/对象 |
Returns
Record
<UnknownMapKey
, any
>
如果没有参数返回空对象
toPairs
▸ toPairs(obj
): any
[]
返回指定对象的所有key,value组成的二维数组
Example
//[['a', 1], ['b', 2], ['c', 3]]console.log(_.toPairs({a:1,b:2,c:3}))
Parameters
Name | Type |
---|---|
obj | Record <UnknownMapKey , any > |
Returns
any
[]
二维数组
unset
▸ unset(obj
, path
): boolean
删除obj上path路径对应属性
Since
2.3
Parameters
Name | Type | Description |
---|---|---|
obj | Record <UnknownMapKey , any > | 需要设置属性值的对象,如果obj不是对象(isObject返回false),直接返回obj |
path | string | number | (string | number ) | 属性路径,可以是索引数字,字符串key,或者多级属性数组 |
Returns
boolean
成功返回true,失败或路径不存在返回false
values
▸ values(obj
): any
返回对象的所有value数组
Example
let f = new Function("this.a=1;this.b=2;");f.prototype.c = 3;//[1,2]console.log(_.values(new f()))
Parameters
Name | Type |
---|---|
obj | Record <UnknownMapKey , any > |
Returns
any
对象根属性对应的值列表
valuesIn
▸ valuesIn(obj
): any
返回对象的所有value数组 包括原型链中的属性
Example
let f = new Function("this.a=1;this.b=2;");f.prototype.c = 3;//[1,2,3]console.log(_.valuesIn(new f()))
Parameters
Name | Type |
---|---|
obj | Record <UnknownMapKey , any > |
Returns
any
对象根属性对应的值列表