forEachResultRow
The forEachResultRow method takes a function that it will then call for each ResultRow in the ResultTable of a query.
forEachResultRow(
queryId: string,
rowCallback: ResultRowCallback,
): void| Type | Description | |
|---|---|---|
queryId | string | The |
rowCallback | ResultRowCallback | The function that should be called for every |
| returns | void | This has no return value. |
This method is useful for iterating over each ResultRow of the ResultTable of the query in a functional style. The rowCallback parameter is a ResultRowCallback function that will be called with the Id of each ResultRow, and with a function that can then be used to iterate over each ResultCell of the ResultRow, should you wish.
Example
This example iterates over each ResultRow in a query's ResultTable.
import {createQueries, createStore} from 'tinybase';
const store = createStore().setTable('pets', {
fido: {species: 'dog', color: 'brown'},
felix: {species: 'cat', color: 'black'},
cujo: {species: 'dog', color: 'black'},
});
const queries = createQueries(store).setQueryDefinition(
'dogColors',
'pets',
({select, where}) => {
select('color');
where('species', 'dog');
},
);
queries.forEachResultRow('dogColors', (rowId, forEachCell) => {
console.log(rowId);
forEachCell((cellId) => console.log(`- ${cellId}`));
});
// -> 'fido'
// -> '- color'
// -> 'cujo'
// -> '- color'
Since
v2.0.0