跳到主要内容

useArrayReduce

分类
导出大小
183 B
上次更改
3 个月前

响应式的 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

与 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

类型声明

显示类型声明
ts
export type 
UseArrayReducer
<
PV
,
CV
,
R
> = (
previousValue
:
PV
,
currentValue
:
CV
,
currentIndex
: number,
) =>
R
/** * Reactive `Array.reduce` * * @see https://vueuse.org.cn/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.cn/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
>

来源

源文件文档

贡献者

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

更新日志

v13.6.0
d32f8 - refactor: 为所有纯函数添加 @__NO_SIDE_EFFECTS__ 注释 (#4907)
v12.8.0
7432f - feat(types): 废弃 MaybeRefMaybeRefOrGetter,转而使用 Vue 的原生类型 (#4636)
v12.3.0
59f75 - feat(toValue): 废弃 @vueuse/shared 中的 toValue,转而使用 Vue 的原生函数
0a9ed - feat!: 放弃对 Vue 2 的支持,优化打包并清理 (#4349)
v11.2.0
ae542 - 修复:initialValue 可以是一个函数 (#4243)

根据 MIT 许可证发布。