Skip to main content

Function: intersect()

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

Creates an iterable with the elements that are in both input iterables.

Type Parameters

Type Parameter
ElementType

Parameters

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

Returns

IterableIterator<ElementType>

Example

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

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

Remarks

This function consumes the entire second iterator to build the set of elements to intersect with first.

Seealso

difference for the opposite behavior.

Defined in

projects/utilities/packages/iterator-utilities/src/lib/intersect.ts:26