Toolbox-XToolbox-X

Find Missing Elements

Finds elements in one array that are missing from another array.

findMissingElements

Performs a deep comparison between two arrays and returns elements missing from one array relative to the other. It allows you to specify the direction of comparison to extract missing values from either array.

Function Signature

function findMissingElements<T, U>(
  array1: T[],
  array2: U[],
  missingFrom: 'from-first' | 'from-second'
): (T | U)[]

Parameters

ParameterTypeDescription
array1T[]First array to compare
array2U[]Second array to compare
missingFrom'from-first' | 'from-second'Direction of comparison:
- 'from-first': Finds elements in array1 missing from array2
- 'from-second': Finds elements in array2 missing from array1

Returns

  • (T | U)[]: An array containing elements present in the source array but missing in the target array (based on deep equality comparison).

Example Usage

playground.ts

Notes

  • Performs deep equality comparison (handles objects and nested structures) using isDeepEqual.
  • Preserves original element references.
  • Returns empty array if no differences found.
  • Works with arrays of different types (T and U).

Comparison Logic

  1. 'from-first' mode:
    array1.filter(item => !array2.some(item2 => isDeepEqual(item, item2)))
  2. 'from-second' mode:
    array2.filter(item => !array1.some(item1 => isDeepEqual(item, item1)))

Last updated: Sun, Jun 14, 2026 07:15:25PM (UTC)

On this page