apps-script-utils 2.1.1 Help

intersect

function intersect<T>(...arrays: T[][]): T[];

Order comes from the first array, and the result is deduplicated: a value repeated there appears once. Values are compared with SameValueZero, the rule a Set uses, so NaN intersects with NaN — which a comparison by === never does — and -0 matches 0.

The arrays after the first each become a set once, and the first is then walked a single time. The cost is linear in the total input rather than the nested scan a hand-written version usually ends up being. The inputs are not modified.

Called with a single array, intersect returns its unique values; called with none, it returns [].

Parameters

Parameter

Type

Description

...arrays

T[][]

The arrays to intersect. Left untouched.

Returns

T[] — a new array of the shared values, in the order of the first array, without duplicates.

Throws

Exception

Condition

TypeError

Any argument is not an array.

Examples

Values shared by every array

intersect([1, 2, 3], [2, 3, 4]); // => [2, 3] intersect([1, 2, 3], [2, 3], [3, 9]); // => [3] intersect([1, 2], [9]); // => []

Finding the rows two sheets have in common

/** * Returns the identifiers that appear in both sheets, in the order of the * first one. */ export function findSharedIds( active: GoogleAppsScript.Spreadsheet.Sheet, billed: GoogleAppsScript.Spreadsheet.Sheet ): string[] { const left = active.getRange("A2:A").getValues().flat() as string[]; const right = billed.getRange("A2:A").getValues().flat() as string[]; return intersect(left, right); }
function findSharedIds(active, billed) { const left = active.getRange("A2:A").getValues().flat(); const right = billed.getRange("A2:A").getValues().flat(); return intersect(left, right); }

Deduplication and edge cases

intersect([1, 1, 2], [1, 2]); // => [1, 2] — the result is deduplicated intersect([NaN], [NaN]); // => [NaN] intersect([1, 2]); // => [1, 2] — a single array, deduplicated intersect(); // => []

See also

Source

src/lang/array/intersect.ts

23 September 2026

This documentation was generated with AI (Claude) from the library's source code.