get
get
(key: PropertyKey) => (anything: any) => any
Return the value found at key
const getA = get('a') getA({ a: 'b' }) // is 'b' getA({}) // is undefined const getFirst = get(0) getFirst(['a']) // is 'a'
const getA = get('a') getA({ a: 'b' }) // is 'b' getA({}) // is undefined const getFirst = get(0) getFirst(['a']) // is 'a'
const getA = get('a') getA({ a: 'b' }) // is 'b' getA({}) // is undefined const getFirst = get(0) getFirst(['a']) // is 'a'
const getA = get('a') getA({ a: 'b' }) // is 'b' getA({}) // is undefined const getFirst = get(0) getFirst(['a']) // is 'a'
Sometimes we need a property's value. Below, we're organizing a conference. We have a list of attendees and are printing pins of everyone's first name. Let's get a list of first names.
const attendees = [ { first: 'liz', last: 'brown' }, { first: 'phil', last: 'smith' }, { first: 'mary', last: 'garcia' }, ] const getFirstName = get('first') const getAllFirstNames = mapValues(getFirstName) const firstNames = getAllFirstNames(attendees) console.log(firstNames) // is [ // liz, // phil, // mary, // ]
type Person = { first: string; last: string } const attendees: Person[] = [ { first: 'liz', last: 'brown' }, { first: 'phil', last: 'smith' }, { first: 'mary', last: 'garcia' }, ] const getFirstName = get('first')<Person> const getAllFirstNames = mapValues(getFirstName)<Person[]> const firstNames = getAllFirstNames(attendees) console.log(firstNames) // is [ // liz, // phil, // mary, // ]
const attendees = [ { first: 'liz', last: 'brown' }, { first: 'phil', last: 'smith' }, { first: 'mary', last: 'garcia' }, ] const getFirstName = get('first') const getAllFirstNames = mapValues(getFirstName) const firstNames = getAllFirstNames(attendees) console.log(firstNames) // is [ // liz, // phil, // mary, // ]
type Person = { first: string; last: string } const attendees: Person[] = [ { first: 'liz', last: 'brown' }, { first: 'phil', last: 'smith' }, { first: 'mary', last: 'garcia' }, ] const getFirstName = get('first')<Person> const getAllFirstNames = mapValues(getFirstName)< Person[] > const firstNames = getAllFirstNames(attendees) console.log(firstNames) // is [ // liz, // phil, // mary, // ]
This utility does not treat Maps differently. It only gets object properties. If you're looking to get the value out of a map, use getValueAtMapKey.