Object.select()

Get an Object property’s value by its path.

Signature

Object.select(object: { *: Any }, path: Array<String>) => Any

Description

Object.select() gets an Object property’s value by its path.

The path is defined as an Array of Strings, where each String represents a property key in the Object’s hierarchy. The method traverses the Object structure following the path to retrieve the desired value.

Parameters

Parameter Type Required Description

object

Object containing fields of Any type

Yes

Object to extract the property from.

path

Array of Strings

Yes

Path to a property in the Object. Each element in the Array represents a level in the Object’s hierarchy.

Return value

Type Description

Any

Value of the property at the path. If the path doesn’t exist or is undefined, the value is null.

Examples

Basic example

// Gets the value of the top-level `foo` property.
Object.select({ foo : 'bar' }, ['foo'])
"bar"

Nested property

// Defines an Object with customer data.
let customer =  {
  "name": "Ruby Von Rails",
  "email": "ruby@example.com",
  "address": {
    "street": "87856 Mendota Court",
    "city": "Washington",
    "state": "DC",
    "postalCode": "20220",
    "country": "US"
  }
}

// Gets the customer's nested `address.state` property.
Object.select(customer, ['address', 'state'])
"DC"

Retrieve a nested Object

// Defines an Object with customer data.
let customer =  {
  "name": "Ruby Von Rails",
  "email": "ruby@example.com",
  "address": {
    "street": "87856 Mendota Court",
    "city": "Washington",
    "state": "DC",
    "postalCode": "20220",
    "country": "US"
  }
}

// Gets the customer's nested `address` property,
// which is an Object.
Object.select(customer, ['address'])
{
  street: "87856 Mendota Court",
  city: "Washington",
  state: "DC",
  postalCode: "20220",
  country: "US"
}

Is this article helpful? 

Tell Fauna how the article can be improved:
Visit Fauna's forums or email docs@fauna.com

Thank you for your feedback!