CtrlK
BlogDocsLog inGet started
Tessl Logo

tessl/npm-array-union

Create an array of unique values, in order, from the input arrays

Pending
Overview
Eval results
Files

Array Union

Array Union creates an array of unique values, in order, from the input arrays. It provides a simple, efficient utility function that flattens multiple arrays and removes duplicates while preserving the order of first occurrence.

Package Information

  • Package Name: array-union
  • Package Type: npm
  • Language: JavaScript (ES modules) with TypeScript definitions
  • Installation: npm install array-union

Core Imports

ES modules (recommended for modern environments):

import arrayUnion from "array-union";

TypeScript:

import arrayUnion from "array-union";
// or
import arrayUnion = require("array-union");

CommonJS:

const arrayUnion = require("array-union");

Basic Usage

import arrayUnion from "array-union";

// Basic array union
arrayUnion([1, 1, 2, 3], [2, 3]);
//=> [1, 2, 3]

// Single array deduplication
arrayUnion(['foo', 'foo', 'bar']);
//=> ['foo', 'bar']

// Multiple arrays with different types
arrayUnion(['🐱', '🦄', '🐻'], ['🦄', '🌈']);
//=> ['🐱', '🦄', '🐻', '🌈']

// Complex example with multiple arrays
arrayUnion(['🐱', '🦄'], ['🐻', '🦄'], ['🐶', '🌈', '🌈']);
//=> ['🐱', '🦄', '🐻', '🐶', '🌈']

Capabilities

Array Union Function

Creates an array of unique values from any number of input arrays, preserving order of first occurrence.

/**
 * Create an array of unique values, in order, from the input arrays
 * @param arguments - Any number of arrays to union
 * @returns Array containing unique values from all input arrays
 */
function arrayUnion<ArgumentsType extends readonly unknown[]>(
  ...arguments: readonly ArgumentsType[]
): ArgumentsType;

Parameters:

  • ...arguments: Rest parameter accepting any number of arrays. Can be mixed types (numbers, strings, objects, etc.)

Returns:

  • Array containing unique values from all input arrays, maintaining the order of first occurrence
  • Return type matches the input array type through TypeScript generics

Behavior:

  • Flattens all input arrays into a single array
  • Removes duplicate values using Set-based deduplication
  • Preserves the order of first occurrence for each unique value
  • Works with any data types (primitives, objects, arrays, etc.)
  • Can be called with no arguments (returns empty array)
  • Can be called with a single array (removes duplicates from that array)

Import Notes:

  • Package uses ES modules ("type": "module" in package.json)
  • TypeScript definitions support both ES module and CommonJS import patterns
  • Default export is the arrayUnion function

Usage Examples:

// Numbers
arrayUnion([1, 2, 2, 3, 1, 2, 4], [1, 2, 3, 6, 7]);
//=> [1, 2, 3, 4, 6, 7]

// Mixed types
arrayUnion([1, 2, 2, 3, 1, 2, 4], ['c', 'a', 'd']);
//=> [1, 2, 3, 4, 'c', 'a', 'd']

// Strings only
arrayUnion(['a', 'a', 'b', 'a'], ['c', 'a', 'd']);
//=> ['a', 'b', 'c', 'd']

// Empty input
arrayUnion();
//=> []

// Single array deduplication
arrayUnion([1, 1, 2, 2, 3]);
//=> [1, 2, 3]

Install with Tessl CLI

npx tessl i tessl/npm-array-union
Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/array-union@3.0.x
Publish Source
CLI
Badge
tessl/npm-array-union badge