高级前端
手写代码
【Q747】如何实现一个 omit/omitBy 函数

如何实现一个 omit/omitBy 函数

更多描述

const object = {
  a: 3,
  b: 4,
  c: 5,
};
 
//=> { c: 5 }
_.omit(object, ["a", "b"]);
 
// omit by value
//=> { b:4, c: 5 }
omitBy(object, (value) => value === 3);

Issue 欢迎在 Gtihub Issue 中回答此问题: Issue 793 (opens in a new tab)

Author 回答者: alexzhang1030 (opens in a new tab)

TypeScript

一种简单思路,顺序遍历 source 的每一项 key,与参数做对比,通过即储存到目标对象中

function omit<T extends Record<string, unknown>>(source: T, keys: (keyof T)[]) {
  return Object.keys(source).reduce((target: T, nowKey: keyof T) => {
    if (!keys.includes(nowKey)) target[nowKey] = source[nowKey];
    return target;
  }, {} as T);
}
 
function omitBy<T extends Record<string, unknown>>(
  source: T,
  filterFn: (v: unknown) => boolean,
) {
  return Object.keys(source).reduce((target: T, nowKey: keyof T) => {
    if (!filterFn(source[nowKey])) target[nowKey] = source[nowKey];
    return target;
  }, {} as T);
}

JavaScript

function omit(source, keys) {
  return Object.keys(source).reduce((target, nowKey) => {
    if (!keys.includes(nowKey)) target[nowKey] = source[nowKey];
    return target;
  }, {});
}
 
function omitBy(source, filiterFn) {
  return Object.keys(source).reduce((target, nowKey) => {
    if (!filiterFn(source[nowKey])) target[nowKey] = source[nowKey];
    return target;
  }, {});
}

Author 回答者: shfshanyue (opens in a new tab)

@alexzhang1030 omit 的复杂度过高,性能较差。应遍历 keys,逐一删除。另外,ts 直接用 Omit 这个内置 type。

Author 回答者: alexzhang1030 (opens in a new tab)

@alexzhang1030 omit 的复杂度过高,性能较差。应遍历 keys,逐一删除。另外,ts 直接用 Omit 这个内置 type。

有个问题,直接删除不就修改源数据了

Author 回答者: shfshanyue (opens in a new tab)

首先 {...obj}

---原始邮件--- 发件人: @._> 发送时间: 2022年10月21日(周五) 中午12:11 收件人: _@.>; 抄送: @.**@.>; 主题: Re: [shfshanyue/Daily-Question] 【Q747】如何实现一个 omit/omitBy 函数 (Issue #793)

@alexzhang1030 omit 的复杂度过高,性能较差。应遍历 keys,逐一删除。另外,ts 直接用 Omit 这个内置 type。

有个问题,直接删除不就修改源数据了

— Reply to this email directly, view it on GitHub, or unsubscribe. You are receiving this because you authored the thread.Message ID: @.***>

Author 回答者: alexzhang1030 (opens in a new tab)

首先 {...obj}

---原始邮件---

发件人: @.***>

发送时间: 2022年10月21日(周五) 中午12:11

收件人: @.***>;

抄送: @.**@.>;

主题: Re: [shfshanyue/Daily-Question] 【Q747】如何实现一个 omit/omitBy 函数 (Issue #793)

@alexzhang1030 omit 的复杂度过高,性能较差。应遍历 keys,逐一删除。另外,ts 直接用 Omit 这个内置 type。

有个问题,直接删除不就修改源数据了

Reply to this email directly, view it on GitHub, or unsubscribe.

You are receiving this because you authored the thread.Message ID: @.***>

懂了,还的是月哥,待会我加上

Author 回答者: croatialu (opens in a new tab)

function omit<
  T extends Record<string, any>,
  K extends string,
  K2 extends keyof T,
>(obj: T, keys: (K | K2)[]) {
  const result = { ...obj };
 
  keys.forEach((key) => {
    delete result[key];
  });
 
  return result as Omit<T, K>;
}
 
function omitBy<T extends Record<string, any>, K extends keyof T>(
  object: T,
  callback: (value: T[K], key: K) => boolean,
) {
  const result = { ...object };
 
  Object.entries(result).forEach(([key, value]) => {
    const isDrop = callback(value, key as K);
 
    if (isDrop) delete result[key];
  });
 
  return result as Partial<T>;
}