Skip to content

useArrayDifference

Category
Export Size
301 B
Last Changed
3 weeks ago

Reactive get array difference of two arrays.

By default, it returns the difference of the first array from the second array, so call A \ B, Relative Complement of B in A.

You can pass the symmetric option to get the Symmetric difference of two arrays A △ B.

Usage

Use with reactive array

ts
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

ts
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 }]

Symmetric Difference

This composable also supports Symmetric difference by passing the symmetric option.

ts
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
,
{
symmetric
: true }
) // result.value: [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 6 }]

Type Declarations

ts
export interface UseArrayDifferenceOptions {
  /**
   * Returns asymmetric difference
   *
   * @see https://en.wikipedia.org/wiki/Symmetric_difference
   * @default false
   */
  
symmetric
?: boolean
} export type
UseArrayDifferenceReturn
<
T
= any> =
ComputedRef
<
T
[]>
export declare function
useArrayDifference
<
T
>(
list
:
MaybeRefOrGetter
<
T
[]>,
values
:
MaybeRefOrGetter
<
T
[]>,
key
?: keyof
T
,
options
?: UseArrayDifferenceOptions,
):
UseArrayDifferenceReturn
<
T
>
export declare function
useArrayDifference
<
T
>(
list
:
MaybeRefOrGetter
<
T
[]>,
values
:
MaybeRefOrGetter
<
T
[]>,
compareFn
?: (
value
:
T
,
othVal
:
T
) => boolean,
options
?: UseArrayDifferenceOptions,
):
UseArrayDifferenceReturn
<
T
>

Source

SourceDocs

Contributors

Anthony Fu
Anthony Fu
SerKo
Robin
IlyaL
wangliangxin
chirokas
simpleoo0o
Lee Dogyeong
丶远方

Changelog

v13.6.0 on
d32f8 - refactor: add @__NO_SIDE_EFFECTS__ annotations to all pure functions (#4907)
v13.1.0 on
c1d6e - feat(shared): ensure return types exists (#4659)
v12.8.0 on
7432f - feat(types): deprecate MaybeRef and MaybeRefOrGetter in favor of Vue's native (#4636)
v12.3.0 on
59f75 - feat(toValue): deprecate toValue from @vueuse/shared in favor of Vue's native
v12.2.0-beta.1 on
46fdc - feat: new symmetric option (#4146)
v12.0.0-beta.1 on
0a9ed - feat!: drop Vue 2 support, optimize bundles and clean up (#4349)
v10.0.0-beta.5 on
cb644 - refactor!: remove isFunction and isString utils
v10.0.0-beta.4 on
4d757 - feat(types)!: rename MaybeComputedRef to MaybeRefOrGetter
0a72b - feat(toValue): rename resolveUnref to toValue
v10.0.0-beta.1 on
41858 - fix: error with falsey (#2869)

Released under the MIT License.

FREE WEEKEND
48 Hours of Unlimited Official Vue.js Certification Training
Reserve Your Spot
23-24 August