Computing new values

You can pass iteratee functions to map() that compute new values. Let's implement a function that capitalizes the string that's passed to it:

const capitalize = s =>
`${s.charAt(0).toUpperCase()}${s.slice(1)}`;

This function will make the first character of any string uppercase. Let's use this as part of an iteratee function passed to map() to help generate a list of names:

const myList = List.of(
Map.of('first', 'joe', 'last', 'brown', 'age', 45),
Map.of('first', 'john', 'last', 'smith', 'age', 32),
Map.of('first', 'mary', 'last', 'wise', 'age', 56)
);
const myMappedList = myList.map(
v => [
capitalize(v.get('first')),
capitalize(v.get('last'))
].join(' ')
);

console.log('myList', myList.toJS());
// -> myList [ { first: 'joe', last: 'brown', age: 45 },
// -> { first: 'john', last: 'smith', age: 32 },
// -> { first: 'mary', last: 'wise', age: 56 } ]
console.log('myMappedList', myMappedList.toJS());
// -> myMappedList [ 'Joe Brown', 'John Smith', 'Mary Wise' ]

Our list is filled with maps that each have the first and last keys. We want to map each of these maps to a string consisting of the capitalized first and last names. The iteratee does this using the capitalize() function and putting the results into an array so that we can join them together. The result is a new list of name strings.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.70.255