Skip to content

intersects

Simple helper that check if two lists shared at least an item in common. null and undefined are treated as empty arrays and return false.

Available since v1.0.0

import { intersects } from '@helpers4/array';
// or, from the all-in-one package (same code, one install):
import { intersects } from 'helpers4/array';
intersects<T>(a: readonly T[] | null | undefined, b: readonly T[] | null | undefined): boolean
ParameterTypeDescription
areadonly T[] | null | undefinedOne list
breadonly T[] | null | undefinedAnother list

boolean — true if one item is in common, false otherwise.

Returns true when at least one element is shared between both arrays.

intersects([1, 2, 3], [3, 4, 5])
// => true

Returns false when no elements are shared.

intersects([1, 2], [3, 4])
// => false

View source on GitHub