Appearance
super-tools-lib - v1.74.0 / Modules / object
Module: object
Table of contents
Functions
Functions
cloneDeep
▸ cloneDeep<T
>(data
): T
深拷贝
- 兼容深度克隆数据类型:Object,Array,RegExp,Date,Map,Set,String,Symbol,Number,Null,Undefined,Boolean,NaN
Since
1.62.0
Example
ts
import { cloneDeep } from 'super-tools-lib'
const obj = {a:{a:{a:1}}, s: obj}
const newObj = cloneDeep(obj)
obj === newObj // false
obj.s === newObj.s // false
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
data | T | 数据 |
Returns
T
返回克隆后的数据
compareObjects
▸ compareObjects<T
, K
>(newObj
, oldObj
): Record
<string
, any
>
比较两个对象的差异
- 以新对象为基准,返回新对象与旧对象的差异
Since
1.71.0
Example
ts
import { compareObjects } from 'super-tools-lib'
const newObj = {
a: 1,
b: 2,
}
const oldObj = {
a: 1,
b: 3,
}
const res = compareObjects(newObj, oldObj)
console.log(res) // { b: 3 }
Type parameters
Name |
---|
T |
K |
Parameters
Name | Type | Description |
---|---|---|
newObj | T | Record <string , T > | 新对象 |
oldObj | K | Record <string , K > | 旧对象 |
Returns
Record
<string
, any
>
返回新对象与旧对象的差异
get
▸ get(object
, path
, defaultValue
): any
根据对象的path路径获取值
Since
1.62.0
Example
ts
import { get } from 'super-tools-lib'
const obj = {a: {b: {c: 1}}, b: {a:[[2]]}}
get(obj, 'a.b.c') // 1
get(obj, 'b.a[0][0]') // 2
get(obj, 'b.a[0][0].a', '2222') // '2222'
Parameters
Name | Type | Description |
---|---|---|
object | Record <string , any > | 要检索的对象 |
path | string | 获取属性的路径 |
defaultValue | any | 获取属性的路径失败时,这值会被返回 |
Returns
any
返回解析的值
has
▸ has(object
, path
): boolean
检查对象是否存在path属性路径
Since
1.62.0
Example
ts
import { has } from 'super-tools-lib'
const obj = {a: {b: {c: 1}}, b: {a:[[2]]}}
has(obj, 'a.b.c') // true
has(obj, 'b.a[0][0]') // true
has(obj, 'b.a[0][0].a') // false
Parameters
Name | Type | Description |
---|---|---|
object | Record <string , any > | 要检索的对象 |
path | string | 属性的路径 |
Returns
boolean
返回 true | false
keys
▸ keys<T
>(obj
): string
[]
将对象的key转换成数组
Since
1.62.0
Example
ts
import { keys } from 'super-tools-lib'
keys({a:2,b:1}) // ['a', 'b']
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
obj | Record <string , T > | 对象 |
Returns
string
[]
返回key数组
objToOrder
▸ objToOrder(obj
): string
对象序列化
Since
1.62.0
Example
ts
import { objToOrder } from 'super-tools-lib'
objToOrder({a:1,b:2,c:3}) // 'a=1&b=2&c=3'
Parameters
Name | Type | Description |
---|---|---|
obj | Record <string , string | number | boolean > | 对象 |
Returns
string
返回序列化的字符串
omit
▸ omit(obj
, keys
): any
忽略对象中指定的key返回新的对象
Example
ts
import { omit } from 'super-tools-lib'
omit({a:1,b:2}, ['a']) // {b:2}
Parameters
Name | Type | Description |
---|---|---|
obj | any | 对象 |
keys | string [] | 忽略的key |
Returns
any
返回忽略指定key的新对象
orderToObj
▸ orderToObj(str
): Object
对象反序列化
Since
1.62.0
Example
ts
import { orderToObj } from 'super-tools-lib'
orderToObj('a=1&b=2&c=3') // {a:1,b:2,c:3}
Parameters
Name | Type | Description |
---|---|---|
str | string | 序列化字符串 |
Returns
Object
返回对象
parse
▸ parse(str
): any
JSON.parse
Example
ts
import { parse } from 'lodash'
parse('{"a":1}') // => {a: 1}
parse('{"a":1') // => false
Parameters
Name | Type | Description |
---|---|---|
str | string | 需要解析的字符串 |
Returns
any
返回解析后的对象
pick
▸ pick(obj
, keys
): Object
从 object 中选中的 key 的对象
Since
1.63.0
Example
ts
import { pick } from 'super-tools-lib'
pick({a:1,b:2}, ['a']) // {a:1}
Parameters
Name | Type | Description |
---|---|---|
obj | any | 对象 |
keys | string [] | 选中的 key |
Returns
Object
返回选中的 key 的对象
set
▸ set(object
, path
, value
): Record
<string
, any
>
根据对象的path路径设置值
Since
1.62.0
Example
ts
import { set } from 'super-tools-lib'
const obj = {a: {b: {c: 1}}, b: {a:[[2]]}}
set(obj, 'a.b.c', 2) // {a: {b: {c: 2}}, b: {a:[[2]]}}
set(obj, 'b.a[0][0]', 3) // {a: {b: {c: 2}}, b: {a:[[3]]}}
#### Parameters
| Name | Type | Description |
| :------ | :------ | :------ |
| `object` | `Record`<`string`, `any`\> | 要检索的对象 |
| `path` | `string` | 获取属性的路径 |
| `value` | `any` | 要设置的值 |
#### Returns
`Record`<`string`, `any`\>
返回设置后的对象
___
### values
▸ **values**<`T`\>(`obj`): `T`[]
将对象的value转换成数组
**`Since`**
1.62.0
**`Example`**
```ts
import { values } from 'super-tools-lib'
values({a:1,b:2}) // [1, 2]
Type parameters
Name |
---|
T |
Parameters
Name | Type | Description |
---|---|---|
obj | Record <string , T > | 对象 |
Returns
T
[]
返回value数组
watch
▸ watch(obj
, prop
, callback
): void
监听对象值的变化
Since
1.67.0
Example
ts
import { watch } from 'super-tools-lib'
const obj = {a: 1}
watch(obj, 'a', (val) => {
console.log(val)
})
obj.a = 2 // 2
Parameters
Name | Type | Description |
---|---|---|
obj | Record <string , any > | 要监听的对象 |
prop | string | 要监听的属性 |
callback | (val : any ) => void | 回调函数 |
Returns
void