getAtPath

getAtPath

  • (path: PropertyKey[]) => (anything: any) => any
  1. Return the value found at path

    const obj = { a: { b: 'c' } }
    const getValueAtB = getAtPath(['a', 'b'])
    getValueAtB(obj) // is 'c'
    getValueAtB({}) // is undefined
    
    const arr = [{ a: 'b' }]
    const getValueAtA = getAtPath([0, 'a'])
    getValueAtA(arr) // is 'b'
    const obj = { a: { b: 'c' } }
    const getValueAtB = getAtPath(['a', 'b'])
    getValueAtB(obj) // is 'c'
    getValueAtB({}) // is undefined
    
    const arr = [{ a: 'b' }]
    const getValueAtA = getAtPath([0, 'a'])
    getValueAtA(arr) // is 'b'
    
    const obj = { a: { b: 'c' } }
    const getValueAtB = getAtPath(['a', 'b'])
    getValueAtB(obj) // is 'c'
    getValueAtB({}) // is undefined
    
    const arr = [{ a: 'b' }]
    const getValueAtA = getAtPath([0, 'a'])
    getValueAtA(arr) // is 'b'
    const obj = { a: { b: 'c' } }
    const getValueAtB = getAtPath(['a', 'b'])
    getValueAtB(obj) // is 'c'
    getValueAtB({}) // is undefined
    
    const arr = [{ a: 'b' }]
    const getValueAtA = getAtPath([0, 'a'])
    getValueAtA(arr) // is 'b'
    
  2. Sometimes we want to reach a deep property. Below, we have a contact list for our team. We're picking everyone up, and they all live in the area, so let's get everyone's streets.

    const contacts = {
      chris: {
        address: {
          street: '123 Main',
          state: 'Maine',
          zip: '04841',
        },
      },
      liz: {
        address: {
          street: '987 Park',
          state: 'Maine',
          zip: '04841',
        },
      },
      phil: {
        address: {
          street: '555 Oak',
          state: 'Maine',
          zip: '04841',
        },
      },
    }
    
    const getStreet = getAtPath(['address', 'street'])
    const getAllStreets = mapValues(getStreet)
    const streetByPerson = getAllStreets(contacts)
    console.log(streetByPerson)
    // is {
    //   chris: 123 Main
    //   liz: 987 Park
    //   phil: 555 Oak
    // }
    type Address = {
      address: {
        street: string
        state: string
        zip: string
      }
    }
    type Directory = Record<string, Address>
    const contacts: Directory = {
      chris: {
        address: {
          street: '123 Main',
          state: 'Maine',
          zip: '04841',
        },
      },
      liz: {
        address: {
          street: '987 Park',
          state: 'Maine',
          zip: '04841',
        },
      },
      phil: {
        address: {
          street: '555 Oak',
          state: 'Maine',
          zip: '04841',
        },
      },
    }
    
    const getStreet = getAtPath(['address', 'street'])<Address>
    const getAllStreets = mapValues(getStreet)<Directory>
    const streetByPerson = getAllStreets(contacts)
    console.log(streetByPerson)
    // is {
    //   chris: 123 Main
    //   liz: 987 Park
    //   phil: 555 Oak
    // }
    
    const contacts = {
      chris: {
        address: {
          street: '123 Main',
          state: 'Maine',
          zip: '04841',
        },
      },
      liz: {
        address: {
          street: '987 Park',
          state: 'Maine',
          zip: '04841',
        },
      },
      phil: {
        address: {
          street: '555 Oak',
          state: 'Maine',
          zip: '04841',
        },
      },
    }
    
    const getStreet = getAtPath(['address', 'street'])
    const getAllStreets = mapValues(getStreet)
    const streetByPerson = getAllStreets(contacts)
    console.log(streetByPerson)
    // is {
    //   chris: 123 Main
    //   liz: 987 Park
    //   phil: 555 Oak
    // }
    type Address = {
      address: {
        street: string
        state: string
        zip: string
      }
    }
    type Directory = Record<string, Address>
    const contacts: Directory = {
      chris: {
        address: {
          street: '123 Main',
          state: 'Maine',
          zip: '04841',
        },
      },
      liz: {
        address: {
          street: '987 Park',
          state: 'Maine',
          zip: '04841',
        },
      },
      phil: {
        address: {
          street: '555 Oak',
          state: 'Maine',
          zip: '04841',
        },
      },
    }
    
    const getStreet = getAtPath([
      'address',
      'street',
    ])<Address>
    const getAllStreets = mapValues(
      getStreet
    )<Directory>
    const streetByPerson = getAllStreets(contacts)
    console.log(streetByPerson)
    // is {
    //   chris: 123 Main
    //   liz: 987 Park
    //   phil: 555 Oak
    // }
    
  3. When resolving the path, this utility does not treat Maps differently. It only gets object properties.