mOmit

mOmit

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

    Consider omit instead.
    This is for the uncommon case where mutating collection is required.

    const obj = { a: 1, b: 2, c: 3, d: 4 }
    const omitAC = mOmit(['a', 'c'])
    const mutatedObj = omitAC(obj)
    console.log(mutatedObj === obj) // is true
    console.log(obj) // is { b: 2, d: 4 }
    const obj = { a: 1, b: 2, c: 3, d: 4 }
    const omitAC = mOmit(['a', 'c'])
    const mutatedObj = omitAC(obj)
    console.log(mutatedObj === obj) // is true
    console.log(obj) // is { b: 2, d: 4 }
    
    const obj = { a: 1, b: 2, c: 3, d: 4 }
    const omitAC = mOmit(['a', 'c'])
    const mutatedObj = omitAC(obj)
    console.log(mutatedObj === obj) // is true
    console.log(obj) // is { b: 2, d: 4 }
    const obj = { a: 1, b: 2, c: 3, d: 4 }
    const omitAC = mOmit(['a', 'c'])
    const mutatedObj = omitAC(obj)
    console.log(mutatedObj === obj) // is true
    console.log(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 = mOmit(['jason', 'amy'])
    removeEmployees(printDesign)
    
    console.log(printDesign)
    // 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 = mOmit(['jason', 'amy'])<Department>
    removeEmployees(printDesign)
    
    console.log(printDesign)
    // 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 = mOmit(['jason', 'amy'])
    removeEmployees(printDesign)
    
    console.log(printDesign)
    // 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 = mOmit([
      'jason',
      'amy',
    ])<Department>
    removeEmployees(printDesign)
    
    console.log(printDesign)
    // is {
    //   matt: { email: 'matt@example.com', startDate: '2024 10 05' }
    //   kim: { email: 'kim@example.com', startDate: '2024 07 19' }
    // }