Loading source
Pulling the file list, source metadata, and syntax-aware rendering for this listing.
Source from repo
Apply VueUse composables in Vue 3/Nuxt projects to replace custom implementations with battle-tested utilities.
Files
Skill
Size
Entrypoint
Format
Open file
Syntax-highlighted preview of this file as included in the skill package.
references/useArrayReduce.md
1---2category: Array3---45# useArrayReduce67Reactive `Array.reduce`.89## Usage1011```ts12import { useArrayReduce } from '@vueuse/core'1314const sum = useArrayReduce([ref(1), ref(2), ref(3)], (sum, val) => sum + val)15// sum.value: 616```1718### Use with reactive array1920```ts21import { useArrayReduce } from '@vueuse/core'2223const list = reactive([1, 2])24const sum = useArrayReduce(list, (sum, val) => sum + val)2526list.push(3)27// sum.value: 628```2930### Use with initialValue3132```ts33import { useArrayReduce } from '@vueuse/core'3435const list = reactive([{ num: 1 }, { num: 2 }])36const sum = useArrayReduce(list, (sum, val) => sum + val.num, 0)37// sum.value: 338```3940## Type Declarations4142```ts43export type UseArrayReducer<PV, CV, R> = (44previousValue: PV,45currentValue: CV,46currentIndex: number,47) => R48export type UseArrayReduceReturn<T = any> = ComputedRef<T>49/**50* Reactive `Array.reduce`51*52* @see https://vueuse.org/useArrayReduce53* @param list - the array was called upon.54* @param reducer - a "reducer" function.55*56* @returns the value that results from running the "reducer" callback function to completion over the entire array.57*58* @__NO_SIDE_EFFECTS__59*/60export declare function useArrayReduce<T>(61list: MaybeRefOrGetter<MaybeRefOrGetter<T>[]>,62reducer: UseArrayReducer<T, T, T>,63): UseArrayReduceReturn<T>64/**65* Reactive `Array.reduce`66*67* @see https://vueuse.org/useArrayReduce68* @param list - the array was called upon.69* @param reducer - a "reducer" function.70* @param initialValue - a value to be initialized the first time when the callback is called.71*72* @returns the value that results from running the "reducer" callback function to completion over the entire array.73*74* @__NO_SIDE_EFFECTS__75*/76export declare function useArrayReduce<T, U>(77list: MaybeRefOrGetter<MaybeRefOrGetter<T>[]>,78reducer: UseArrayReducer<U, T, U>,79initialValue: MaybeRefOrGetter<U>,80): UseArrayReduceReturn<U>81```82