indexArray
Maps each position of a list to a row.
Maps each position of a list to a row. Rows are created and removed only when the length
changes; the row at position i stays, and its item() accessor follows the value at i.
Import#
ts
import { indexArray } from "@shadow-js/core";
Signature#
ts
function indexArray<T, U>(
list: ListSource<T>,
map: (item: () => T, index: number) => U,
options?: { fallback?: () => U },
): Accessor<U[]>
Parameters#
| Name | Description |
|---|---|
list |
Source list. null, undefined and false are an empty list. |
map |
Runs once per position, untracked, in the row's own root. |
options |
fallback is shown while the list is empty. Undefined: an empty list maps to []. |
Returns#
Accessor of the mapped rows, in the order of the input.
Throws#
- Nothing itself. Reading the accessor rethrows what
mapor a row cleanup threw; several errors at once arrive as oneAggregateError.
Examples#
ts
import { createRoot, indexArray, useSignal } from "@shadow-js/core";
createRoot(() => {
const [values] = useSignal(["a", "b"]);
const cells = indexArray(values, (value, index) => `${index}: ${value()}`);
console.log(cells()); // ["0: a", "1: b"]
});