When catalog data is nested several levels deep, direct property access quickly becomes noisy. _.get retrieves a value by following an information path: an ordered list of keys naming nested entities. section 3.3
Retrieve a book title
Given catalogData.booksByIsbn["978-1779501127"].title, write the path as ["booksByIsbn", "978-1779501127", "title"] and retrieve the title with _.get(catalogData, ["booksByIsbn", "978-1779501127", "title"]). The result is "Watchmen". section 3.3
The same small operation supports larger data-oriented functions:
- Read a book's authorIds with _.get(book, "authorIds"), then resolve each identifier through a second path. section 3.4
- Build a BookInfo record by retrieving title and isbn fields while composing authorNames from related catalog data. section 3.4
- Implement the basic mechanism with a loop that starts at the input object and indexes through each path component. section 3.3
- Expect a trade-off in statically typed languages: retrieving a heterogeneous map value may require casting before concrete string operations. section 3.3
Listing 3.5 A Catalog record
var catalogData = {
"booksByIsbn": {
"978-1779501127": {
"isbn": "978-1779501127",
"title": "Watchmen",
"publicationYear": 1987,
"authorIds": ["alan-moore", "dave-gibbons"],
"bookItems": [
{
"id": "book-item-1",
"libId": "nyc-central-lib",
"isLent": true
},
{
"id": "book-item-2",
"libId": "nyc-central-lib",
"isLent": false
}
]
}
},
"authorsById": {
"alan-moore": {
"name": "Alan Moore",
"bookIsbns": ["978-1779501127"]
},
"dave-gibbons": {
"name": "Dave Gibbons",
"bookIsbns": ["978-1779501127"]
}
}
}
Inspect the catalog record as nested data, then trace how an information path reaches a specific title. section 3.3
