TinyBase logoTinyBase

forEachMetric

The forEachMetric method takes a function that it will then call for each Metric in the Metrics object.

forEachMetric(metricCallback: MetricCallback): void
TypeDescription
metricCallbackMetricCallback

The function that should be called for every Metric.

returnsvoid

This has no return value.

This method is useful for iterating over all the Metrics in a functional style. The metricCallback parameter is a MetricCallback function that will be called with the Id of each Metric and its value.

Example

This example iterates over each Metric in a Metrics object.

const store = createStore().setTable('species', {
  dog: {price: 5},
  cat: {price: 4},
  worm: {price: 1},
});
const metrics = createMetrics(store)
  .setMetricDefinition('highestPrice', 'species', 'max', 'price')
  .setMetricDefinition('lowestPrice', 'species', 'min', 'price');

metrics.forEachMetric((metricId, metric) => {
  console.log([metricId, metric]);
});
// -> ['highestPrice', 5]
// -> ['lowestPrice', 1]