Back to Functions

without

arrays

Creates an array excluding all given values.

Installation

Import
import { without } from '@tulx/utils';

Source Code

Implementation
/**
 * Creates an array excluding all given values.
 *
 * @param array - The array to filter.
 * @param values - The values to exclude.
 * @returns The new array of filtered values.
 *
 * @example
 * ```ts
 * without([2, 1, 2, 3], 1, 2); // [3]
 * ```
 */
export function without<T>(array: readonly T[], ...values: readonly T[]): T[] {
  const valueSet = new Set(values);
  return array.filter((item) => !valueSet.has(item));
}

Example

import { without } from '@tulx/utils';

without([2, 1, 2, 3], 1, 2); // [3]

Related Functions