Skip to content

intersection

Input two arrays arr1 and arr2, and return their intersection.

Accepts a getter, which can be a field path of getByPath or a callback function, used to provide an identifier to distinguish elements.

Added in v0.0.1

Usage

ts
import { intersection } from 'parsnip-kit'

intersection([1, 2, 3, NaN], [1, 4, 8, NaN]) // [1, NaN]

intersection(
  [{ v: 1 }, { v: 2 }, { v: 3 }],
  [{ v: 1 }, { v: 4 }, { v: 8 }],
  'v'
) // [{ v: 1 }]

intersection(
  [{ v: [1] }, { v: [2] }, { v: [3] }],
  [{ v: [1] }, { v: [4] }, { v: [8] }],
  'v[0]'
) // [{ v: [1] }]

intersection([1.1, 2.4, 3.9, 4.16], [1, 2, 3, 4, 5, 6], Math.floor) // [1.1, 2.4, 3.9, 4.16]

intersection([1.1, 2.4, 3.9, 4.16], [1, 2, 3, 4, 5, 6], (item: number, index: number, arr: number[]) => {
  return Math.floor(item)
}) // [1.1, 2.4, 3.9, 4.16]

API

Type Parameter

ArgTypeDescription
TType of elements of array

Arguments

ArgTypeOptionalDefaultDescription
arr1T[]falseundefinedArray to be intersected
arr2T[]falseundefinedArray to be intersected
getterstring | ((item: T, index: number, arr: T[]) => any)trueundefinedProvide an identifier to distinguish the elements

Returns

Type
T[]