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/useArrayDifference.md
1---2category: Array3---45# useArrayDifference67Reactive get array difference of two arrays.89By default, it returns the difference of the first array from the second array, so call `A \ B`, [Relative Complement](<https://en.wikipedia.org/wiki/Complement_(set_theory)>) of B in A.1011You can pass the `symmetric` option to get the [Symmetric difference](https://en.wikipedia.org/wiki/Symmetric_difference) of two arrays `A △ B`.1213## Usage1415### Use with reactive array1617```ts18import { useArrayDifference } from '@vueuse/core'1920const list1 = ref([0, 1, 2, 3, 4, 5])21const list2 = ref([4, 5, 6])22const result = useArrayDifference(list1, list2)23// result.value: [0, 1, 2, 3]24list2.value = [0, 1, 2]25// result.value: [3, 4, 5]26```2728### Use with reactive array and use function comparison2930```ts31import { useArrayDifference } from '@vueuse/core'3233const list1 = ref([{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }])34const list2 = ref([{ id: 4 }, { id: 5 }, { id: 6 }])3536const result = useArrayDifference(list1, list2, (value, othVal) => value.id === othVal.id)37// result.value: [{ id: 1 }, { id: 2 }, { id: 3 }]38```3940### Symmetric Difference4142This composable also supports [Symmetric difference](https://en.wikipedia.org/wiki/Symmetric_difference) by passing the `symmetric` option.4344```ts {10}45import { useArrayDifference } from '@vueuse/core'4647const list1 = ref([{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }])48const list2 = ref([{ id: 4 }, { id: 5 }, { id: 6 }])4950const result = useArrayDifference(51list1,52list2,53(value, othVal) => value.id === othVal.id,54{ symmetric: true }55)56// result.value: [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 6 }]57```5859## Type Declarations6061```ts62export interface UseArrayDifferenceOptions {63/**64* Returns asymmetric difference65*66* @see https://en.wikipedia.org/wiki/Symmetric_difference67* @default false68*/69symmetric?: boolean70}71export type UseArrayDifferenceReturn<T = any> = ComputedRef<T[]>72export declare function useArrayDifference<T>(73list: MaybeRefOrGetter<T[]>,74values: MaybeRefOrGetter<T[]>,75key?: keyof T,76options?: UseArrayDifferenceOptions,77): UseArrayDifferenceReturn<T>78export declare function useArrayDifference<T>(79list: MaybeRefOrGetter<T[]>,80values: MaybeRefOrGetter<T[]>,81compareFn?: (value: T, othVal: T) => boolean,82options?: UseArrayDifferenceOptions,83): UseArrayDifferenceReturn<T>84```85