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/useArrayFilter.md
1---2category: Array3---45# useArrayFilter67Reactive `Array.filter`89## Usage1011### Use with array of multiple refs1213```ts14import { useArrayFilter } from '@vueuse/core'1516const item1 = ref(0)17const item2 = ref(2)18const item3 = ref(4)19const item4 = ref(6)20const item5 = ref(8)21const list = [item1, item2, item3, item4, item5]22const result = useArrayFilter(list, i => i % 2 === 0)23// result.value: [0, 2, 4, 6, 8]24item2.value = 125// result.value: [0, 4, 6, 8]26```2728### Use with reactive array2930```ts31import { useArrayFilter } from '@vueuse/core'3233const list = ref([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])34const result = useArrayFilter(list, i => i % 2 === 0)35// result.value: [0, 2, 4, 6, 8]36list.value.shift()37// result.value: [2, 4, 6, 8]38```3940## Type Declarations4142```ts43export type UseArrayFilterReturn<T = any> = ComputedRef<T[]>44/**45* Reactive `Array.filter`46*47* @see https://vueuse.org/useArrayFilter48* @param list - the array was called upon.49* @param fn - a function that is called for every element of the given `list`. Each time `fn` executes, the returned value is added to the new array.50*51* @returns a shallow copy of a portion of the given array, filtered down to just the elements from the given array that pass the test implemented by the provided function. If no elements pass the test, an empty array will be returned.52*53* @__NO_SIDE_EFFECTS__54*/55export declare function useArrayFilter<T, S extends T>(56list: MaybeRefOrGetter<MaybeRefOrGetter<T>[]>,57fn: (element: T, index: number, array: T[]) => element is S,58): UseArrayFilterReturn<S>59export declare function useArrayFilter<T>(60list: MaybeRefOrGetter<MaybeRefOrGetter<T>[]>,61fn: (element: T, index: number, array: T[]) => unknown,62): UseArrayFilterReturn<T>63```64