Skip to content

useArrayReduce

类别
导出体积
183 B
上次更改
2 months ago

响应式 Array.reduce

用法

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

与响应式数组一起使用

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

与初始值一起使用

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

类型声明

ts
export type 
UseArrayReducer
<
PV
,
CV
,
R
> = (
previousValue
:
PV
,
currentValue
:
CV
,
currentIndex
: number,
) =>
R
/** * 响应式 `Array.reduce` * * @see https://vueuse.org/useArrayReduce * @param list - 被调用的数组。 * @param reducer - 一个“reducer”函数。 * * @returns 在整个数组上运行“reducer”回调函数完成后的值。 * * @__NO_SIDE_EFFECTS__ */ export declare function
useArrayReduce
<
T
>(
list
:
MaybeRefOrGetter
<
MaybeRefOrGetter
<
T
>[]>,
reducer
:
UseArrayReducer
<
T
,
T
,
T
>,
):
ComputedRef
<
T
>
/** * 响应式 `Array.reduce` * * @see https://vueuse.org/useArrayReduce * @param list - 被调用的数组。 * @param reducer - 一个“reducer”函数。 * @param initialValue - 在第一次调用回调函数时初始化的值。 * * @returns 在整个数组上运行“reducer”回调函数完成后的值。 * * @__NO_SIDE_EFFECTS__ */ export declare function
useArrayReduce
<
T
,
U
>(
list
:
MaybeRefOrGetter
<
MaybeRefOrGetter
<
T
>[]>,
reducer
:
UseArrayReducer
<
U
,
T
,
U
>,
initialValue
:
MaybeRefOrGetter
<
U
>,
):
ComputedRef
<
U
>

源码

源码文档

贡献者

一纸忘忧
Anthony Fu
Anthony Fu
SerKo
IlyaL
Mutter
Levi (Nguyễn Lương Huy)
XLor

更新日志

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