TinyBase logoTinyBase

forEachSlice

The forEachSlice method takes a function that it will then call for each Slice in a specified Index.

forEachSlice(
  indexId: Id,
  sliceCallback: SliceCallback,
): void
TypeDescription
indexIdId

The Id of the Index to iterate over.

sliceCallbackSliceCallback

The function that should be called for every Slice.

returnsvoid

This has no return value.

This method is useful for iterating over the Slice structure of the Index in a functional style. The rowCallback parameter is a RowCallback function that will be called with the Id and value of each Row in the Slice.

Example

This example iterates over each Row in a Slice, and lists its Id.

const store = createStore().setTable('pets', {
  fido: {species: 'dog'},
  felix: {species: 'cat'},
  cujo: {species: 'dog'},
});
const indexes = createIndexes(store);
indexes.setIndexDefinition('bySpecies', 'pets', 'species');

indexes.forEachSlice('bySpecies', (sliceId, forEachRow) => {
  console.log(sliceId);
  forEachRow((rowId) => console.log(`- ${rowId}`));
});
// -> 'dog'
// -> '- fido'
// -> '- cujo'
// -> 'cat'
// -> '- felix'