Multiple map() calls

Let's revisit our earlier example where we transformed two map values into a single capitalized name string:

const capitalize = s =>
`${s.charAt(0).toUpperCase()}${s.slice(1)}`;
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)
);

myList
.toSeq()
.map(v => v.update('first', capitalize))
.map(v => v.update('last', capitalize))
.map(v => [v.get('first'), v.get('last')].join(' '))
.forEach(v => console.log('name', v));
// -> name Joe Brown
// -> name John Smith
// -> name Mary Wise

In this version of the example, we're using sequences and side-effects to render the names. Once your list is converted to a sequence, you can call map() as many times as you like without creating new collections. Remember, values pass through sequence operations one at a time.

The first map() call updates the first key using the capitalize() function. The second map() call does the same thing, except with the last key. The final map() call before the side-effect joins the two key values together to form the resulting string.

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

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