set

set

  • (key: EntryKey, value: any) => (collection: EntryCollection) => EntryCollection
  1. Set an entry key to a value

    const obj = { a: 'b' }
    const setAToC = set('a', 'c')
    setAToC(obj) // is { a: 'c' }
    
    const arr = ['a', '_', 'c']
    const setIdx1ToB = set(1, 'b')
    setIdx1ToB(arr) // is ['a', 'b', 'c']
    const obj = { a: 'b' }
    const setAToC = set('a', 'c')
    setAToC(obj) // is { a: 'c' }
    
    const arr = ['a', '_', 'c']
    const setIdx1ToB = set(1, 'b')
    setIdx1ToB(arr) // is ['a', 'b', 'c']
    
    const obj = { a: 'b' }
    const setAToC = set('a', 'c')
    setAToC(obj) // is { a: 'c' }
    
    const arr = ['a', '_', 'c']
    const setIdx1ToB = set(1, 'b')
    setIdx1ToB(arr) // is ['a', 'b', 'c']
    const obj = { a: 'b' }
    const setAToC = set('a', 'c')
    setAToC(obj) // is { a: 'c' }
    
    const arr = ['a', '_', 'c']
    const setIdx1ToB = set(1, 'b')
    setIdx1ToB(arr) // is ['a', 'b', 'c']
    
  2. Sometimes we want to set a key to a value. Below, we are building an array of cast members for True Detective Season 1. Let's add a property 'isLead' using the set of lead members.

    const topBilledCast = [
      { name: 'Matthew McConaughey' },
      { name: 'Woody Harrelson' },
      { name: 'Michelle Monaghan' },
      { name: 'Michael Potts' },
      { name: 'Tory Kittles' },
    ]
    
    const leads = new Set(['Matthew McConaughey', 'Woody Harrelson'])
    const flagLead = set('isLead', true)
    const updateActor = actor => {
      return leads.has(actor.name) ? flagLead(actor) : actor
    }
    const flagAllLeads = mapValues(updateActor)
    const updatedCast = flagAllLeads(topBilledCast)
    
    console.log(updatedCast)
    // is [
    //   { name: Matthew McConaughey, isLead: true }
    //   { name: Woody Harrelson, isLead: true }
    //   { name: Michelle Monaghan }
    //   { name: Michael Potts }
    //   { name: Tory Kittles }
    // ]
    type Actor = {
      name: string
      isLead?: true
    }
    const topBilledCast: Actor[] = [
      { name: 'Matthew McConaughey' },
      { name: 'Woody Harrelson' },
      { name: 'Michelle Monaghan' },
      { name: 'Michael Potts' },
      { name: 'Tory Kittles' },
    ]
    
    const leads = new Set(['Matthew McConaughey', 'Woody Harrelson'])
    const flagLead = set('isLead', true)<Actor>
    const updateActor = (actor: Actor) => {
      return leads.has(actor.name) ? flagLead(actor) : actor
    }
    const flagAllLeads = mapValues(updateActor)<Actor[]>
    const updatedCast = flagAllLeads(topBilledCast)
    
    console.log(updatedCast)
    // is [
    //   { name: Matthew McConaughey, isLead: true }
    //   { name: Woody Harrelson, isLead: true }
    //   { name: Michelle Monaghan }
    //   { name: Michael Potts }
    //   { name: Tory Kittles }
    // ]
    
    const topBilledCast = [
      { name: 'Matthew McConaughey' },
      { name: 'Woody Harrelson' },
      { name: 'Michelle Monaghan' },
      { name: 'Michael Potts' },
      { name: 'Tory Kittles' },
    ]
    
    const leads = new Set([
      'Matthew McConaughey',
      'Woody Harrelson',
    ])
    const flagLead = set('isLead', true)
    const updateActor = actor => {
      return leads.has(actor.name) ?
          flagLead(actor)
        : actor
    }
    const flagAllLeads = mapValues(updateActor)
    const updatedCast = flagAllLeads(topBilledCast)
    
    console.log(updatedCast)
    // is [
    //   { name: Matthew McConaughey, isLead: true }
    //   { name: Woody Harrelson, isLead: true }
    //   { name: Michelle Monaghan }
    //   { name: Michael Potts }
    //   { name: Tory Kittles }
    // ]
    type Actor = {
      name: string
      isLead?: true
    }
    const topBilledCast: Actor[] = [
      { name: 'Matthew McConaughey' },
      { name: 'Woody Harrelson' },
      { name: 'Michelle Monaghan' },
      { name: 'Michael Potts' },
      { name: 'Tory Kittles' },
    ]
    
    const leads = new Set([
      'Matthew McConaughey',
      'Woody Harrelson',
    ])
    const flagLead = set('isLead', true)<Actor>
    const updateActor = (actor: Actor) => {
      return leads.has(actor.name) ?
          flagLead(actor)
        : actor
    }
    const flagAllLeads = mapValues(updateActor)<
      Actor[]
    >
    const updatedCast = flagAllLeads(topBilledCast)
    
    console.log(updatedCast)
    // is [
    //   { name: Matthew McConaughey, isLead: true }
    //   { name: Woody Harrelson, isLead: true }
    //   { name: Michelle Monaghan }
    //   { name: Michael Potts }
    //   { name: Tory Kittles }
    // ]