Skip to content

union

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

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 { union } from 'parsnip-kit'

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

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

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

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

union([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, 5, 6]

API

Type Parameter

ArgTypeDescription
TType of elements of array

Arguments

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

Returns

Type
T[]