mapValues
mapValues
(mapperFn: MapperFn) => (collection: Collection) => Collection
Change each value of a collection
const arr = [1, 2, 3] const inc = n => n + 1 const incAll = mapValues(inc) incAll(arr) // is [2, 3, 4]
const arr = [1, 2, 3] const inc = (n: number) => n + 1 const incAll = mapValues(inc) incAll(arr) // is [2, 3, 4]
const arr = [1, 2, 3] const inc = n => n + 1 const incAll = mapValues(inc) incAll(arr) // is [2, 3, 4]
const arr = [1, 2, 3] const inc = (n: number) => n + 1 const incAll = mapValues(inc) incAll(arr) // is [2, 3, 4]
We often want to change the values of a collection. Below, we're organizing a conference. We have a list of attendees and need their first names for name tags. Let's map the attendees to their 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, // ]