Product
Introducing SSO
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
arr-union
Advanced tools
Package description
The arr-union npm package is designed to create an array of unique values, in order, from the provided arrays. It is particularly useful for combining arrays while removing duplicate entries efficiently.
Union of multiple arrays
This feature allows you to pass multiple arrays to the union function, which returns a new array that includes every unique element from the input arrays, preserving the order they appear in the input.
const union = require('arr-union');
let result = union([1, 2], [2, 3], [3, 4]);
console.log(result); // Output: [1, 2, 3, 4]
Lodash's union function offers similar functionality to arr-union. It creates an array of unique values, in order, from all given arrays using SameValueZero for equality comparisons. Compared to arr-union, lodash.union is part of a larger utility library, which might be preferable for projects that require additional utility functions beyond array union.
Underscore.js also provides a union function that computes the union of the passed-in arrays: the list of unique items, in order, that are present in one or more of the arrays. Like lodash, Underscore is a more comprehensive toolkit, which might be overkill if you only need the union functionality.
Readme
Combines a list of arrays, returning a single array with unique values, using strict equality for comparisons.
You can do this with just [].concat.apply([], arguments)
, like array-union but this implementation is much faster and more peformant.
For the most common use cases, this library is 20 times faster than array-union.
#1: five-arrays.js
array-union.js x 289,891 ops/sec ±0.70% (97 runs sampled)
current.js x 5,429,077 ops/sec ±0.81% (93 runs sampled)
#2: ten-arrays.js
array-union.js x 161,132 ops/sec ±0.72% (98 runs sampled)
current.js x 1,725,939 ops/sec ±0.61% (97 runs sampled)
#3: two-arrays.js
array-union.js x 376,238 ops/sec ±0.83% (95 runs sampled)
current.js x 6,817,593 ops/sec ±0.63% (94 runs sampled)
See the benchmarks.
npm i arr-union --save
var last = require('array-last');
union(['a'], ['b', 'c'], ['d', 'e', 'f']);
//=> ['a', 'b', 'c', 'd', 'e', 'f']
Returns only unique elements:
union(['a', 'a'], ['b', 'c']);
//=> ['a', 'b', 'c']
Install dev dependencies.
npm i -d && npm test
Pull requests and stars are always welcome. For bugs and feature requests, please create an issue
Jon Schlinkert
Copyright (c) 2015 Jon Schlinkert
Released under the MIT license
This file was generated by verb-cli on March 25, 2015.
FAQs
Combines a list of arrays, returning a single array with unique values, using strict equality for comparisons.
The npm package arr-union receives a total of 10,929,015 weekly downloads. As such, arr-union popularity was classified as popular.
We found that arr-union demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Product
Streamline your login process and enhance security by enabling Single Sign-On (SSO) on the Socket platform, now available for all customers on the Enterprise plan, supporting 20+ identity providers.
Security News
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.