TinyBase

getMetric

The getMetric method gets the current value of a Metric.

getMetric(metricId: string): undefined | number
TypeDescription
metricIdstring

The Id of the Metric.

returnsundefined | number

The numeric value of the Metric, or `undefined`.

If the identified Metric does not exist (or if the definition references a Table or Cell value that does not exist) then undefined is returned.

Example

This example creates a Store, creates a Metrics object, and defines a simple Metric to average the price values in the Table. It then uses getMetric to access its value (and also the value of a Metric that has not been defined).

const store = createStore().setTable('species', {
  dog: {price: 5},
  cat: {price: 4},
  worm: {price: 1},
});

const metrics = createMetrics(store);
metrics.setMetricDefinition('highestPrice', 'species', 'max', 'price');

console.log(metrics.getMetric('highestPrice'));
// -> 5
console.log(metrics.getMetric('lowestPrice'));
// -> undefined