主题
useArrayDifference
反应式获取两个数组的数组差异
¥Reactive get array difference of two arrays
用法
¥Usage
与反应式数组一起使用
¥Use with reactive array
js
import { useArrayDifference } from '@vueuse/core'
const list1 = ref([0, 1, 2, 3, 4, 5])
const list2 = ref([4, 5, 6])
const result = useArrayDifference(list1, list2)
// result.value: [0, 1, 2, 3]
list2.value = [0, 1, 2]
// result.value: [3, 4, 5]
与反应式数组一起使用并使用函数比较
¥Use with reactive array and use function comparison
js
import { useArrayDifference } from '@vueuse/core'
const list1 = ref([{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 }, { id: 5 }])
const list2 = ref([{ id: 4 }, { id: 5 }, { id: 6 }])
const result = useArrayDifference(list1, list2, (value, othVal) => value.id === othVal.id)
// result.value: [{ id: 1 }, { id: 2 }, { id: 3 }]
类型声明
typescript
export declare function useArrayDifference<T>(
list: MaybeRefOrGetter<T[]>,
values: MaybeRefOrGetter<T[]>,
key?: keyof T,
): ComputedRef<T[]>
export declare function useArrayDifference<T>(
list: MaybeRefOrGetter<T[]>,
values: MaybeRefOrGetter<T[]>,
compareFn?: (value: T, othVal: T) => boolean,
): ComputedRef<T[]>