Skip to content

equalsUnordered

Order-independent (set-style) array equality.

Two arrays are considered equal when they have the same length and every element of arr1 has at least one structural match in arr2 (and vice versa via the length check). Nested arrays are compared recursively with the same order-independent semantics. Nested plain objects are compared with equalsShallow from object/. All other values use strict equality (===).

Use this when the inputs represent unordered collections (sets, tags…). For positional equality use equalsShallow or equalsDeep from this category.

Available since v2.0.0

import { equalsUnordered } from '@helpers4/array';
// or, from the all-in-one package (same code, one install):
import { equalsUnordered } from 'helpers4/array';
equalsUnordered<T>(arr1: readonly T[], arr2: readonly T[]): boolean
ParameterTypeDescription
arr1readonly T[]First array
arr2readonly T[]Second array

boolean — true if both arrays contain the same items regardless of order, false otherwise.

Compare identical arrays regardless of order

Section titled “Compare identical arrays regardless of order”

Returns true when both arrays contain the same elements, in any order.

equalsUnordered([1, 2, 3], [3, 2, 1])
// => true

Returns false when arrays contain different elements.

equalsUnordered([1, 2], [1, 3])
// => false

Supports shallow comparison of nested objects.

equalsUnordered([{ a: 1 }], [{ a: 1 }])
// => true

View source on GitHub