Skip to main content

Function: difference()

difference<ElementType>(first: IterableResolvable<ElementType>, second: IterableResolvable<ElementType>): IterableIterator<ElementType>

Creates an iterable with the elements of the first iterable that are not in the second iterable.

Type Parameters

Type Parameter
ElementType

Parameters

ParameterTypeDescription
firstIterableResolvable<ElementType>An iterator to return elements from.
secondIterableResolvable<ElementType>An iterator that contains elements to exclude from the result.

Returns

IterableIterator<ElementType>

Example

import { difference } from '@sapphire/iterator-utilities';

const first = [1, 2, 3, 4, 5];
const second = [3, 4, 5, 6, 7];
console.log([...difference(first, second)]);
// Output: [1, 2]

Remarks

This function consumes the entire second iterator to build the set of elements to exclude from first.

Defined in

projects/utilities/packages/iterator-utilities/src/lib/difference.ts:25