Skip to Content
Mastering JavaScript Functional Programming - Second Edition
book

Mastering JavaScript Functional Programming - Second Edition

by Federico Kereki
January 2020
Intermediate to advanced
470 pages
11h 13m
English
Packt Publishing
Content preview from Mastering JavaScript Functional Programming - Second Edition

Getting a property

Back in the Getting a property from an object section of Chapter 6, Producing Functions – Higher-Order Functions, we wrote a simple getField() function that could handle getting a single attribute out of an object. (See question 6.5 in that chapter for the missing companion setField() function.) Let's take a look at how we can code this. We can have a straightforward version, as follows:

const getField = attr => obj => obj[attr];

We can even go one better by applying currying so that we have a more general version:

const getField = curry((attr, obj) => obj[attr]);

We could get a deep attribute out of an object by composing a series of applications of getField() calls, but that would be rather cumbersome. Instead, let's ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Mastering JavaScript Functional Programming

Mastering JavaScript Functional Programming

Federico Kereki

Publisher Resources

ISBN: 9781839213069Supplemental Content