getValueAtMapKey
getValueAtMapKey
(key: any) => (aMap: map) => Value | undefined
Return the value found at the map's key
const aMap = new Map([ ['a', 1], ['b', 2], ]) const getA = getValueAtMapKey('a') getA(aMap) // is 1 const emptyMap = new Map() getA(emptyMap) // is undefined
const aMap = new Map([ ['a', 1], ['b', 2], ]) const getA = getValueAtMapKey('a') getA(aMap) // is 1 const emptyMap = new Map() getA(emptyMap) // is undefined
const aMap = new Map([ ['a', 1], ['b', 2], ]) const getA = getValueAtMapKey('a') getA(aMap) // is 1 const emptyMap = new Map() getA(emptyMap) // is undefined
const aMap = new Map([ ['a', 1], ['b', 2], ]) const getA = getValueAtMapKey('a') getA(aMap) // is 1 const emptyMap = new Map() getA(emptyMap) // is undefined
Sometimes we need a map's value. Below, we're programming a new screen for New York metro (MTA) users. We need to display their balance and status. MTA stores data in separate tables with the person's object as the key. To test the screen, let's get Tom's data.
const tom = { first: 'tom', last: 'nelson' } const ken = { first: 'ken', last: 'roberts' } const chris = { first: 'chris', last: 'reyes' } const metroBalance = new Map([ [tom, '21.95'], [ken, '15.30'], [chris, '34.50'], ]) const metroSubscriptionStatus = new Map([ [tom, 'active'], [ken, 'paused'], [chris, 'active'], ]) const metroData = { balance: metroBalance, status: metroSubscriptionStatus, } const getTomsValue = getValueAtMapKey(tom) const getTomsData = mapValues(getTomsValue) const tomsData = getTomsData(metroData) console.log(tomsData) // is { // balance: 21.95 // status: active // }
type Person = { first: string; last: string } const tom: Person = { first: 'tom', last: 'nelson' } const ken: Person = { first: 'ken', last: 'roberts' } const chris: Person = { first: 'chris', last: 'reyes' } const metroBalance = new Map([ [tom, '21.95'], [ken, '15.30'], [chris, '34.50'], ]) const metroSubscriptionStatus = new Map([ [tom, 'active'], [ken, 'paused'], [chris, 'active'], ]) type MetroData = { balance: Map<Person, string> status: Map<Person, string> } const metroData: MetroData = { balance: metroBalance, status: metroSubscriptionStatus, } const getTomsValue = getValueAtMapKey(tom)<string> const getTomsData = mapValues(getTomsValue)<MetroData> const tomsData = getTomsData(metroData) console.log(tomsData) // is { // balance: 21.95 // status: active // }
const tom = { first: 'tom', last: 'nelson' } const ken = { first: 'ken', last: 'roberts' } const chris = { first: 'chris', last: 'reyes' } const metroBalance = new Map([ [tom, '21.95'], [ken, '15.30'], [chris, '34.50'], ]) const metroSubscriptionStatus = new Map([ [tom, 'active'], [ken, 'paused'], [chris, 'active'], ]) const metroData = { balance: metroBalance, status: metroSubscriptionStatus, } const getTomsValue = getValueAtMapKey(tom) const getTomsData = mapValues(getTomsValue) const tomsData = getTomsData(metroData) console.log(tomsData) // is { // balance: 21.95 // status: active // }
type Person = { first: string; last: string } const tom: Person = { first: 'tom', last: 'nelson', } const ken: Person = { first: 'ken', last: 'roberts', } const chris: Person = { first: 'chris', last: 'reyes', } const metroBalance = new Map([ [tom, '21.95'], [ken, '15.30'], [chris, '34.50'], ]) const metroSubscriptionStatus = new Map([ [tom, 'active'], [ken, 'paused'], [chris, 'active'], ]) type MetroData = { balance: Map<Person, string> status: Map<Person, string> } const metroData: MetroData = { balance: metroBalance, status: metroSubscriptionStatus, } const getTomsValue = getValueAtMapKey(tom)<string> const getTomsData = mapValues( getTomsValue )<MetroData> const tomsData = getTomsData(metroData) console.log(tomsData) // is { // balance: 21.95 // status: active // }