liveBook · Manning

_.get

As explained in Data-Oriented Programming: Reduce software complexity

_.get is a Lodash function that retrieves a value from a nested object or array using a property path.

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

How Data-Oriented Programming: Reduce software complexity covers it

How deep it goes
Defines information paths, demonstrates retrieval, and sketches a loop implementation before deferring deeper typing concerns.
Examples
Uses a library catalog, the Watchmen record, author identifiers, BookInfo construction, and title-search results.
What it assumes
Assumes nested record data while teaching information paths from zero through a catalog example.
What you can do after
Apply it via Lodash to retrieve nested fields and assemble catalog search results from related records.
How it connects
Connects nested data, information paths, Lodash, map, filter, search functions, and static type casting.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Data-Oriented Programming: Reduce software complexity. The section links point to the parts of the book it draws on.