UNPKG

diginext-utils

Version:
24 lines 740 B
/** * Creates an array of values that are present in all given arrays. * Returns elements that exist in every array. * * @template T - The type of elements in the arrays * @param array1 - First array * @param array2 - Second array * @returns A new array containing only values present in both arrays * * @example * ```ts * intersection([1, 2, 3], [2, 3, 4]); // [2, 3] * intersection(['a', 'b'], ['b', 'c']); // ['b'] * intersection([1, 2], [3, 4]); // [] * ``` */ export function intersection(array1, array2) { if (!Array.isArray(array1) || !Array.isArray(array2)) { return []; } const set2 = new Set(array2); return array1.filter((item) => set2.has(item)); } //# sourceMappingURL=intersection.js.map