omit()
function omit<T, Keys>(obj, keys): Omit<T, Keys>;
Defined in: object/omit.ts:24
从对象中排除指定的属性,创建并返回一个新对象 包含源对象的所有自有属性,除了指定要排除的属性 当输入对象为null/undefined时返回空对象,当排除列表为空时返回原对象的浅拷贝
Type Parameters
| Type Parameter | Description |
|---|---|
T extends Record<string, any> | 输入对象的类型 |
Keys extends string | number | symbol | 要排除的属性键集合类型 |
Parameters
| Parameter | Type | Description |
|---|---|---|
obj | T | 源对象,如果为null/undefined则返回空对象 |
keys | Keys[] | 要排除的属性键数组 |
Returns
Omit<T, Keys>
排除指定属性后的新对象
Examples
omit({ a: 1, b: 2, c: 3 }, ['b']);
// 返回 { a: 1, c: 3 }
omit({ name: 'John', age: 30, email: 'john@example.com' }, ['age', 'email']);
// 返回 { name: 'John' }
omit({ a: 1 }, []); // 返回 { a: 1 }(排除空数组)
omit(null, ['a']); // 返回 {}(输入为null)
**Note:** 函数会创建源对象的浅拷贝,原对象不会被修改
**Note:** 只排除自有属性,继承属性不受影响