omit

omit

  • (keys: EntryKey[]) => (collection: EntryCollection) => EntryCollection
  1. Remove entries from a collection by key

    const obj = { a: 1, b: 2, c: 3, d: 4 }
    const omitAC = omit(['a', 'c'])
    
    omitAC(obj) // is { b: 2, d: 4 }
    const obj = { a: 1, b: 2, c: 3, d: 4 }
    const omitAC = omit(['a', 'c'])
    
    omitAC(obj) // is { b: 2, d: 4 }
    
    const obj = { a: 1, b: 2, c: 3, d: 4 }
    const omitAC = omit(['a', 'c'])
    
    omitAC(obj) // is { b: 2, d: 4 }
    const obj = { a: 1, b: 2, c: 3, d: 4 }
    const omitAC = omit(['a', 'c'])
    
    omitAC(obj) // is { b: 2, d: 4 }
    
  2. Sometimes we want to remove entries by key. Below, we have employees in our print design department. Jason and Amy are moving to web design, so let's remove them from our records and wish them luck :)

    const printDesign = {
      matt: { email: 'matt@example.com', startDate: '2024 10 05' },
      jason: { email: 'jason@example.com', startDate: '2023 08 12' },
      amy: { email: 'amy@example.com', startDate: '2023 04 27' },
      kim: { email: 'kim@example.com', startDate: '2024 07 19' },
    }
    
    const removeEmployees = omit(['jason', 'amy'])
    const updatedPrintDesign = removeEmployees(printDesign)
    
    console.log(updatedPrintDesign)
    // is {
    //   matt: {
    //     email: matt@example.com
    //     startDate: 2024 10 05
    //   }
    //   kim: {
    //     email: kim@example.com
    //     startDate: 2024 07 19
    //   }
    // }
    type Employee = {
      email: string
      startDate: string
    }
    type Department = Record<string, Employee>
    const printDesign: Department = {
      matt: { email: 'matt@example.com', startDate: '2024 10 05' },
      jason: { email: 'jason@example.com', startDate: '2023 08 12' },
      amy: { email: 'amy@example.com', startDate: '2023 04 27' },
      kim: { email: 'kim@example.com', startDate: '2024 07 19' },
    }
    
    const removeEmployees = omit(['jason', 'amy'])<Department>
    const updatedPrintDesign = removeEmployees(printDesign)
    
    console.log(updatedPrintDesign)
    // is {
    //   matt: {
    //     email: matt@example.com
    //     startDate: 2024 10 05
    //   }
    //   kim: {
    //     email: kim@example.com
    //     startDate: 2024 07 19
    //   }
    // }
    
    const printDesign = {
      matt: {
        email: 'matt@example.com',
        startDate: '2024 10 05',
      },
      jason: {
        email: 'jason@example.com',
        startDate: '2023 08 12',
      },
      amy: {
        email: 'amy@example.com',
        startDate: '2023 04 27',
      },
      kim: {
        email: 'kim@example.com',
        startDate: '2024 07 19',
      },
    }
    
    const removeEmployees = omit(['jason', 'amy'])
    const updatedPrintDesign =
      removeEmployees(printDesign)
    
    console.log(updatedPrintDesign)
    // is {
    //   matt: {
    //     email: matt@example.com
    //     startDate: 2024 10 05
    //   }
    //   kim: {
    //     email: kim@example.com
    //     startDate: 2024 07 19
    //   }
    // }
    type Employee = {
      email: string
      startDate: string
    }
    type Department = Record<string, Employee>
    const printDesign: Department = {
      matt: {
        email: 'matt@example.com',
        startDate: '2024 10 05',
      },
      jason: {
        email: 'jason@example.com',
        startDate: '2023 08 12',
      },
      amy: {
        email: 'amy@example.com',
        startDate: '2023 04 27',
      },
      kim: {
        email: 'kim@example.com',
        startDate: '2024 07 19',
      },
    }
    
    const removeEmployees = omit([
      'jason',
      'amy',
    ])<Department>
    const updatedPrintDesign =
      removeEmployees(printDesign)
    
    console.log(updatedPrintDesign)
    // is {
    //   matt: {
    //     email: matt@example.com
    //     startDate: 2024 10 05
    //   }
    //   kim: {
    //     email: kim@example.com
    //     startDate: 2024 07 19
    //   }
    // }