Skip to content

omit (Lodash 兼容性)

请使用 es-toolkitomit

这个 omit 函数由于深拷贝和调用 unset 函数而相对较慢。

请改用更快、更现代的 es-toolkitomit

创建一个排除指定键的新对象。

typescript
const result = omit(obj, ...keys);

参考

omit(object, ...paths)

创建一个排除指定键的新对象。支持深层键路径,可以使用数组一次指定多个键。适用于从对象中删除敏感信息或仅选择需要的属性。

typescript
import { omit } from 'es-toolkit/compat';

// 删除基本键
const user = { id: 1, name: 'John', email: 'john@example.com', password: 'secret' };
const publicUser = omit(user, 'password', 'email');
// 结果: { id: 1, name: 'John' }

// 使用数组删除多个键
const data = { a: 1, b: 2, c: 3, d: 4 };
const filtered = omit(data, ['a', 'c']);
// 结果: { b: 2, d: 4 }

// 删除深层键路径
const nested = {
  user: { profile: { name: 'John', age: 30 }, settings: { theme: 'dark' } },
  admin: true,
};
const result = omit(nested, 'user.profile.age', 'admin');
// 结果: { user: { profile: { name: 'John' }, settings: { theme: 'dark' } } }

// 组合嵌套数组和键
const complex = { a: 1, b: 2, c: 3, d: { e: 4, f: 5 } };
const simplified = omit(complex, 'a', ['b', 'c'], 'd.f');
// 结果: { d: { e: 4 } }

您可以自由组合数组、字符串和键路径。

typescript
import { omit } from 'es-toolkit/compat';

const config = {
  api: { url: 'https://api.example.com', key: 'secret', timeout: 5000 },
  ui: { theme: 'dark', language: 'en' },
  debug: true,
};

// 以多种方式指定键
const cleaned = omit(config, 'api.key', ['debug'], 'ui.language');
// 结果: { api: { url: 'https://api.example.com', timeout: 5000 }, ui: { theme: 'dark' } }

nullundefined 被视为空对象。

typescript
import { omit } from 'es-toolkit/compat';

omit(null, 'key'); // {}
omit(undefined, 'key'); // {}

参数

  • object (T | null | undefined): 要删除键的源对象。
  • ...paths (Array<Many<PropertyKey>>): 要删除的键。可以指定单个键、键数组或深层键路径。

返回值

(Partial<T>): 返回删除指定键后的新对象。

采用 MIT 许可证发布。