Coder Social home page Coder Social logo

Comments (6)

alexzhang1030 avatar alexzhang1030 commented on May 27, 2024

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
    }), {})
}

from daily-question.

shfshanyue avatar shfshanyue commented on May 27, 2024

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

from daily-question.

alexzhang1030 avatar alexzhang1030 commented on May 27, 2024

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

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

from daily-question.

shfshanyue avatar shfshanyue commented on May 27, 2024

from daily-question.

alexzhang1030 avatar alexzhang1030 commented on May 27, 2024

首先 {...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: @.***>

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

from daily-question.

croatialu avatar croatialu commented on May 27, 2024
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>
}


from daily-question.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.