Skip to content

equalsShallow

Positional, one-level (shallow) array equality.

Two arrays are equal when they have the same length and each pair of elements at the same index satisfies strict equality (===). No recursion: nested arrays/objects are compared by reference.

For recursive structural comparison use equalsDeep. For order-independent comparison use equalsUnordered.

Available since v2.0.0

import { equalsShallow } from '@helpers4/array';
equalsShallow<T>(arrA: readonly T[], arrB: readonly T[]): boolean
ParameterTypeDescription
arrAreadonly T[]First array to compare
arrBreadonly T[]Second array to compare

boolean — true if every element matches by === at the same index, false otherwise.

Uses JSON.stringify for a fast shallow comparison.

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

Unlike equals, equalsShallow is order-sensitive.

equalsShallow([1, 2], [2, 1])
// => false

View source on GitHub