Methods
(static) allKeys(object) → {Array.<(string|symbol)>}
- Description:
创建一个数组,包含对象自身的可枚举属性(包含
Symbol
属性)。同
Object.keys
+Object.getOwnPropertySymbols
。注意:
Symbol
键属性在字符串属性后面。
- Source:
- Since:
- 1.1.0
Example
function Foo(){
this.a = 1;
this[Symbol.for('b')] = 2;
}
Foo.prototype.c = 3;
Foo.prototype[Symbol.for('d')] = 4;
allKeys(new Foo); // ['a', Symbol(b)]
Parameters:
Name | Type | Description |
---|---|---|
object |
Object | 要查询的对象。 |
Returns:
对象自身的可枚举属性(包含 Symbol
属性)。
- Type
- Array.<(string|symbol)>
(static) allKeysIn(object) → {Array.<(string|symbol)>}
- Description:
创建一个数组,包含对象自身及继承的可枚举属性(包含
Symbol
属性)。同
key...in
+ 递归原型Object.getOwnPropertySymbols
。注意:
Symbol
键属性在字符串属性后面。
- Source:
- Since:
- 1.1.0
Example
function Foo(){
this.a = 1;
this[Symbol.for('b')] = 2;
}
Foo.prototype.c = 3;
Foo.prototype[Symbol.for('d')] = 4;
allKeysIn(new Foo); // ['a', 'c', Symbol(b), Symbol(d)]
Parameters:
Name | Type | Description |
---|---|---|
object |
Object | 要查询的对象。 |
Returns:
对象自身及继承的可枚举属性(包含 Symbol
属性)。
- Type
- Array.<(string|symbol)>
(static) findKey(obj, predicateopt)
- Description:
查找对象的键。
- Source:
- Since:
- 1.13.0
Example
const obj = { foo: 'bar', baz: 42 }
findKey(obj, isNumber); // 'baz'
findKey(obj, v => typeof v === 'bar'); // 'foo'
const map = new Map([['foo', 'bar'], ['baz', 42]]);
findKey(map, isNumber); // 'baz'
findKey(map, v => typeof v === 'bar'); // 'foo'
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
obj |
Object | Map | 对象或 Map 对象 |
|
predicate |
function |
<optional> |
迭代对象自身的可枚举属性(包含 |
Returns:
如果对象存在要查找的值,返回该值的键,否则返回 undefined
。
(static) fromPathPairs(value, customizeropt) → {Object|Array}
- Description:
将属性路径/值对的数组转为对象。与
pathPairs
相反。如果属性路径为有效索引数字(数字或字符串)且没有值时,将创建数组,否则创建对象。例如:
fromPathPairs([[[0], 42], [["foo"], "baz"]]); // [42, foo: 'baz'] // 调整顺序后 fromPathPairs([[["foo"], "baz"], [[0], 42]]); // { foo: 'baz', '0': 42 }
- Source:
- Since:
- 1.16.0
Example
fromPathPairs([
[['date', 'start'], '2024-10-10'],
[['date', 'end'], '2024-12-31']
]);
// { date: { start: '2024-10-10', end: '2024-12-31' } }
fromPathPairs([
[['date', 0], '2024-10-10'],
[['date', 1], '2024-12-31']
]);
// { date: ['2024-10-10', '2024-12-31'] }
fromPathPairs([
[[0, 'date'], '2024-10-10'],
[[1, 'date'], '2024-12-31']
]);
// [{ date: '2024-10-10' }, { date: '2024-12-31' }]
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
value |
Array | 属性路径/值对的数组。 |
|
customizer |
function |
<optional> |
自定义指定值。 |
Returns:
转换后的对象或数组。
- Type
- Object | Array
(static) get(object, path, defaultValueopt) → {*}
Example
const obj = { a: [{ b: { c: 1 } }] };
get(obj, 'a[0].b.c'); // 1
get(obj, ['a', '0', 'b', 'c']); // 1
get(obj, 'a.b.c', 'default'); // 'default'
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
object |
* | 要查询的对象。 |
|
path |
string | number | symbol | Array | 属性路径字符串或数组。 |
|
defaultValue |
* |
<optional> |
替代返回 |
Returns:
属性值。
- Type
- *
(static) invert(object, predicateopt) → {Object}
- Description:
创建一个对象,该对象由
object
自身可枚举属性(包含Symbol
属性)和值反转组成。
- Source:
- Since:
- 1.8.0
- See:
Example
invert({ a: 1, b: 2 }); // { 1: 'a', 2: 'b' }
invert({ a: undefined, b: null }); // { undefined: 'a', null: 'b' }
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
object |
Object | 来源对象。 |
|
predicate |
function |
<optional> |
调用每一个属性的函数,返回 |
Returns:
新对象。
- Type
- Object
(static) keys(object) → {Array.<string>}
- Description:
创建一个数组,包含对象自身的可枚举属性(不包含
Symbol
属性)。同
Object.keys
。
- Source:
- Since:
- 1.7.0
Example
function Foo(){
this.a = 1;
this[Symbol.for('b')] = 2;
}
Foo.prototype.c = 3;
Foo.prototype[Symbol.for('d')] = 4;
keys(new Foo); // ['a']
Parameters:
Name | Type | Description |
---|---|---|
object |
Object | 要查询的对象。 |
Returns:
对象自身的可枚举属性(不包含 Symbol
属性)。
- Type
- Array.<string>
(static) keysIn(object) → {Array.<string>}
- Description:
创建一个数组,包含对象自身及继承的可枚举属性(不包含
Symbol
属性)。同
key...in
。
- Source:
- Since:
- 1.1.0
Example
function Foo(){
this.a = 1;
this[Symbol.for('b')] = 2;
}
Foo.prototype.c = 3;
Foo.prototype[Symbol.for('d')] = 4;
keysIn(new Foo); // ['a', 'c']
Parameters:
Name | Type | Description |
---|---|---|
object |
Object | 要查询的对象。 |
Returns:
对象自身及继承的可枚举属性(不包含 Symbol
属性)。
- Type
- Array.<string>
(static) merge(object, source, customizeropt, getKeysopt) → {Object}
- Description:
递归合并
source
来源对象自身的可枚举属性(包含Symbol
属性)到object
目标对象。如果目标值存在,被解析为
undefined
的source
来源对象属性将被跳过。数组和普通对象会递归合并,其他对象和值会被直接分配覆盖。如果不需要合并数组,第三个参数传入
merge.NOT_MERGE_ARRAYS
。如果需要合并继承的属性,第四个参数传入 allKeys 方法,
merge(object, source, undefined, allKeys)
。
- Source:
- Since:
- 1.0.0
Example
merge({c: 3}, {e: 5}); // { c: 3, e: 5 }
merge({ a: 1 }, { a: undefined, b: undefined }); // { a: 1, b: undefined }
const object = {
a: [{b: 2}, {d: 4}]
}
const other = {
a: [{c: 3},{e: 5}]
}
merge(object, other); // { a: [{b: 2, c: 3}, {d: 4, e: 5}] }
// 数组不合并
merge(object, other, merge.NOT_MERGE_ARRAYS); // { a: [{c: 3},{e: 5}] }
// 或自定义数组不合并方法
merge(object, other, (objValue, srcValue) => isArray(srcValue) ? srcValue : undefined); // { a: [{c: 3},{e: 5}] }
Parameters:
Name | Type | Attributes | Default | Description |
---|---|---|---|---|
object |
Object | Array | 目标对象。 |
||
source |
Object | Array | 来源对象。 |
||
customizer |
function |
<optional> |
自定义赋值函数,如果函数返回 |
|
getKeys |
function |
<optional> |
allKeys
|
自定义获取对象键方法。默认 |
Returns:
目标对象。
- Type
- Object
(static) mergeObject(object, …args)
- Description:
递归合并
source
来源对象自身的可枚举属性(包含Symbol
属性)到object
目标对象。与
merge
方法的区别:- 不合并数组,如果只传入
2
个参数,同merge(obj, src, merge.NOT_MERGE_ARRAYS)
- 不支持自定义赋值函数和获取对象键,但支持多个来源对象参数
- 不合并数组,如果只传入
- Source:
- Since:
- 1.15.0
Example
mergeObject({c: 3}, {e: 5}); // { c: 3, e: 5 }
mergeObject({ a: 1 }, { a: undefined, b: undefined }); // { a: 1, b: undefined }
mergeObject({ a: 1 }, { a: undefined, b: undefined }, {c:'x'}); // { a: 1, b: undefined, c: 'x' }
const source = {
a: [{b: 2}, {d: 4}]
}
const otherSource = {
a: [{c: 3},{e: 5}]
}
mergeObject({}, source, otherSource); // { a: [{c: 3}, {e: 5}] }
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
object |
Object | Array | 目标对象。 |
|
args |
object |
<repeatable> |
来源对象。 |
Returns:
目标对象。
(static) omit(object, fieldsopt) → {Object}
- Description:
创建一个对象,该对象由忽略属性之外的
object
自身和继承的可枚举属性组成。与pick
相反。
- Source:
- Since:
- 1.0.0
Example
const obj = { name: "jeff", age: 18 };
// 浅拷贝对象
omit(obj); // { name: "jeff", age: 18 }
// 排除单个属性
omit(obj, 'name'); // { age: 18 }
// 排除多个属性
omit(obj, ['name', 'age']); // {}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
object |
Object | 来源对象。 |
|
fields |
string | Array.<string> |
<optional> |
要被忽略的属性。 |
Returns:
新对象。
- Type
- Object
(static) omitBy(object, predicateopt) → {Object}
- Description:
创建一个对象,该对象忽略
predicate
(断言函数)判断不是真值的属性后,object
自身和继承的可枚举属性组成。与pickBy
相反。predicate
调用时会传入两个参数value
key
。
- Source:
- Since:
- 1.0.0
- See:
Example
const obj = { name: "jeff", age: 18 };
omitBy(obj); // { name: "jeff", age: 18 }
omitBy(obj, (value) => typeof value === 'number'); // { name: "jeff" }
omitBy(obj, (value) => value); // {}
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
object |
Object | 来源对象。 |
|
predicate |
function |
<optional> |
调用每一个属性的函数,返回 |
Returns:
新对象。
- Type
- Object
(static) pathPairs(object) → {Array}
- Description:
将对象转为属性路径/值对的数组。与
fromPathPairs
相反。
- Source:
- Since:
- 1.16.0
Example
pathPairs({ date: { start: '2024-10-10', end: '2024-12-31' } });
// [
// [['date', 'start'], '2024-10-10'],
// [['date', 'end'], '2024-12-31']
// ]
pathPairs({ date: ['2024-10-10', '2024-12-31'] });
// [
// [['date', 0], '2024-10-10'],
// [['date', 1], '2024-12-31']
// ]
pathPairs([{ date: '2024-10-10' }, { date: '2024-12-31' }]);
// [
// [[0, 'date'], '2024-10-10'],
// [[1, 'date'], '2024-12-31']
// ]
Parameters:
Name | Type | Description |
---|---|---|
object |
Object | Array | 对象或数组。 |
Returns:
属性路径/值对的数组。
- Type
- Array
(static) pick(object, fieldsopt) → {Object}
Example
const obj = { name: "jeff", age: 18 };
pick(obj); // {}
// 选取单个属性
pick(obj, 'name'); // { name: "jeff" }
// 选取多个属性
pick(obj, ['name', 'age']); // { name: "jeff", age: 18 }
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
object |
Object | 来源对象。 |
|
fields |
string | Array.<string> |
<optional> |
选中的属性。 |
Returns:
新对象。
- Type
- Object
(static) pickBy(obj, predicateopt) → {Object}
- Description:
创建一个对象,该对象的属性从
object
中经predicate
(断言函数)判断为真值的属性。predicate
调用时会传入两个参数value
key
。
- Source:
- Since:
- 1.0.0
- See:
Example
const obj = { name: "jeff", age: 18 };
pickBy(obj); // {}
pickBy(obj, (value) => typeof value === 'number'); // { age: 18 }
pickBy(obj, (value) => value); // { name: "jeff", age: 18 }
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
obj |
Object | 来源对象。 |
|
predicate |
function |
<optional> |
调用每一个属性的函数,返回 |
Returns:
新对象。
- Type
- Object
(static) set(object, path, value, customizeropt)
Example
const obj = {};
set(obj, 'a.b', 1);
console.log(obj); // { a: { b: 1 } }
set(obj, ['x', '0', 'y'], 2);
console.log(obj); // { a: { b: 1 }, x: [{ y: 2 }] }
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
object |
Object | Array | 对象或数组。 |
|
path |
string | number | symbol | Array | 属性路径字符串或数组。 |
|
value |
* | 要设置的值。 |
|
customizer |
function |
<optional> |
自定义指定值。 |
Returns:
object
。
(static) unset(object, path) → {boolean}
Example
const obj = { a: [{ b: { c: 1 } }] };
unset(obj, 'a[0].b.c'); // true
console.log(obj); // { a: [{ b: {} }] }
unset(obj, ['a', '0', 'b', 'c']); // true
console.log(obj); // { a: [{ b: {} }] }
Parameters:
Name | Type | Description |
---|---|---|
object |
* | 要修改的对象。 |
path |
string | number | symbol | Array | 属性路径字符串或数组。 |
Returns:
如果删除成功返回true
,否则返回false
。
- Type
- boolean