Spec-Zone.ru › Valibot

reduceItems

Creates a reduce items transformation action.

const Action = v.reduceItems<TInput, TOutput>(operation, initial);

Generics

  • TInput extends ArrayInput
  • TOutput extends any

Parameters

  • operation (output: TOutput, item: TInput[number], index: number, array: TInput) => TOutput
  • initial TOutput

Explanation

With reduceItems you can apply an operation to each item in an array to reduce it to a single value.

Returns

  • Action ReduceItemsAction<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

pipe

Utils

isOfKind, isOfType

© Fabian Hiller
Licensed under the MIT License.
https://valibot.dev/api/reduceItems/

Spec-Zone.ru

Настройки Оффлайн Что нового Помощь О нас
Spec-Zone .ru
спецификации, руководства, описания, API