Skip to content
On this page

useArrayReduce

Category
Export Size
246 B
Last Changed
6 months ago

Reactive Array.reduce.

Usage

js
import { useArrayReduce } from '@vueuse/core'

const sum = useArrayReduce([ref(1), ref(2), ref(3)], (sum, val) => sum + val)
// sum.value: 6
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

js
import { useArrayReduce } from '@vueuse/core'

const list = reactive([1, 2])
const sum = useArrayReduce(list, (sum, val) => sum + val)

list.push(3)
// sum.value: 6
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

js
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
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
typescript
export type UseArrayReducer<PV, CV, R> = (
  previousValue: PV,
  currentValue: CV,
  currentIndex: number
) => R
/**
 * Reactive `Array.reduce`
 *
 * @see https://vueuse.org/useArrayReduce
 * @param {Array} 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.
 */
export declare function useArrayReduce<T>(
  list: MaybeComputedRef<MaybeComputedRef<T>[]>,
  reducer: UseArrayReducer<T, T, T>
): ComputedRef<T>
/**
 * Reactive `Array.reduce`
 *
 * @see https://vueuse.org/useArrayReduce
 * @param {Array} 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.
 */
export declare function useArrayReduce<T, U>(
  list: MaybeComputedRef<MaybeComputedRef<T>[]>,
  reducer: UseArrayReducer<U, T, U>,
  initialValue: MaybeComputedRef<U>
): ComputedRef<U>
export type UseArrayReducer<PV, CV, R> = (
  previousValue: PV,
  currentValue: CV,
  currentIndex: number
) => R
/**
 * Reactive `Array.reduce`
 *
 * @see https://vueuse.org/useArrayReduce
 * @param {Array} 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.
 */
export declare function useArrayReduce<T>(
  list: MaybeComputedRef<MaybeComputedRef<T>[]>,
  reducer: UseArrayReducer<T, T, T>
): ComputedRef<T>
/**
 * Reactive `Array.reduce`
 *
 * @see https://vueuse.org/useArrayReduce
 * @param {Array} 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.
 */
export declare function useArrayReduce<T, U>(
  list: MaybeComputedRef<MaybeComputedRef<T>[]>,
  reducer: UseArrayReducer<U, T, U>,
  initialValue: MaybeComputedRef<U>
): ComputedRef<U>

Source

SourceDocs

Contributors

Anthony Fu
Levi (Nguyễn Lương Huy)
XLor

Changelog

v9.1.1 on 8/23/2022
0fdbb - fix(shared): resolve internal circular reference
v8.9.4 on 7/17/2022
79e44 - feat: new function (#1919)

Released under the MIT License.