Skip to content

useArrayReduce

Category
Export Size
183 B
Last Changed
2 months ago

Reactive Array.reduce.

Usage

ts
import { 
useArrayReduce
} from '@vueuse/core'
const
sum
=
useArrayReduce
([
ref
(1),
ref
(2),
ref
(3)], (
sum
,
val
) =>
sum
+
val
)
// sum.value: 6

Use with reactive array

ts
import { 
useArrayReduce
} from '@vueuse/core'
const
list
=
reactive
([1, 2])
const
sum
=
useArrayReduce
(
list
, (
sum
,
val
) =>
sum
+
val
)
list
.
push
(3)
// sum.value: 6

Use with initialValue

ts
import { 
useArrayReduce
} from '@vueuse/core'
const
list
=
reactive
([{
num
: 1 }, {
num
: 2 }])
const
sum
=
useArrayReduce
(
list
, (
sum
,
val
) =>
sum
+
val
.
num
, 0)
// sum.value: 3

Type Declarations

Show Type Declarations
ts
export type 
UseArrayReducer
<
PV
,
CV
,
R
> = (
previousValue
:
PV
,
currentValue
:
CV
,
currentIndex
: number,
) =>
R
/** * Reactive `Array.reduce` * * @see https://vueuse.org/useArrayReduce * @param list - the array was called upon. * @param reducer - a "reducer" function. * * @returns the value that results from running the "reducer" callback function to completion over the entire array. * * @__NO_SIDE_EFFECTS__ */ export declare function
useArrayReduce
<
T
>(
list
:
MaybeRefOrGetter
<
MaybeRefOrGetter
<
T
>[]>,
reducer
:
UseArrayReducer
<
T
,
T
,
T
>,
):
ComputedRef
<
T
>
/** * Reactive `Array.reduce` * * @see https://vueuse.org/useArrayReduce * @param list - the array was called upon. * @param reducer - a "reducer" function. * @param initialValue - a value to be initialized the first time when the callback is called. * * @returns the value that results from running the "reducer" callback function to completion over the entire array. * * @__NO_SIDE_EFFECTS__ */ export declare function
useArrayReduce
<
T
,
U
>(
list
:
MaybeRefOrGetter
<
MaybeRefOrGetter
<
T
>[]>,
reducer
:
UseArrayReducer
<
U
,
T
,
U
>,
initialValue
:
MaybeRefOrGetter
<
U
>,
):
ComputedRef
<
U
>

Source

SourceDocs

Contributors

Anthony Fu
Anthony Fu
SerKo
NoiseFan
IlyaL
Mutter
Levi (Nguyễn Lương Huy)
XLor

Changelog

v13.6.0 on
d32f8 - refactor: add @__NO_SIDE_EFFECTS__ annotations to all pure functions (#4907)
v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v12.3.0 on
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v11.2.0 on
ae542 - fix: initialValue can be a function (#4243)

Released under the MIT License.

Turn AI into a coding partner
Claim 40% off for AIDD Masterclass.
Get discount