reduceItems
Creates a reduce items transformation action.
const Action = v.reduceItems<TInput, TOutput>(operation, initial);
Generics
TInputextends ArrayInputTOutputextends any
Parameters
operation(output: TOutput, item: TInput[number], index: number, array: TInput) => TOutputinitialTOutput
Explanation
With reduceItems you can apply an operation to each item in an array to reduce it to a single value.
Returns
ActionReduceItemsAction<TInput, TOutput>
Examples
The following examples show how reduceItems can be used.
Sum all numbers
Schema that sums all the numbers in an array.
const SumArraySchema = v.pipe(
v.array(v.number()),
v.reduceItems((sum, item) => sum + item, 0)
);
Related
The following APIs can be combined with reduceItems.
Schemas
any, array, custom, instance, tuple, unknown
Methods
Utils
© Fabian Hiller
Licensed under the MIT License.
https://valibot.dev/api/reduceItems/