Object.hasPath()
Test if an Object has a property.
Examples
Basic example
fqlCopied!
// Test if the Object contains the top-level `foo` property.
Object.hasPath({ foo : 'bar' }, ['foo'])
true
Property that doesn’t exist
fqlCopied!
// Test if the Object contains the top-level `baz` property.
Object.hasPath({ foo : 'bar' }, ['baz'])
false
Nested property
fqlCopied!
// 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"
}
}
// Test if the customer Object contains the
// nested `address.state` property.
Object.hasPath(customer, ['address', 'state'])
true
Nested property that doesn’t exist
fqlCopied!
// 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"
}
}
// Test if the customer Object contains the
// nested `address.zipCode` property.
Object.hasPath(customer, ['address', 'zipCode'])
false
Format results for dynamic projection
The following getFormatter()
user-defined function (UDF) uses
Object.hasPath()
to determine whether a predefined formatterMap
object
contains a property with a matching collection name. If not, it returns an
abort error.
The getList()
UDF calls getFormatter()
to return the projection for the
provided collection.
The UDFs are used for dynamic projection.
fslCopied!
// Defines the `getFormatter()` UDF.
// Accepts a collection name and returns a
// format for the collection.
function getFormatter(collName) {
// Defines an object with a format
// for each accepted collection name.
let formatterMap = {
Product: product => product {
name,
description,
price },
Category: category => category {
name,
description
}
}
// Use abort() to return an error if the
// collection name doesn't have a format.
if (!Object.hasPath(formatterMap, [collName])) {
abort("No formatter named '#{collName}'")
}
formatterMap[collName]
}
// Returns the collection's data using the
// predefined colleciton.
function getList(collName) {
let collection = Collection(collName)
// Calls the previous `getFormatter()` UDF.
let formatFn = getFormatter(collName)
collection.all().map(formatFn)
}