forEachSlice
The forEachSlice method takes a function that it will then call for each Slice in a specified Index.
forEachSlice(
indexId: string,
sliceCallback: SliceCallback,
): void| Type | Description | |
|---|---|---|
indexId | string | |
sliceCallback | SliceCallback | The function that should be called for every |
| returns | void | 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.
import {createIndexes, createStore} from 'tinybase';
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'
Since
v1.0.0