With the arrival of ES6 with sets and spread operator, you can write the following cryptic one liner:

var a = [34, 35, 45, 48, 49];
var b = [48, 55];
var union = [...new Set([...a, ...b])];
console.log(union);
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Little explanation about this line: [...a, ...b] concatenates two arrays, you can use a.concat(b) as well. new Set() create a set out of it and thus your union. And the last [...x] converts it back to an array.

Answer from Salvador Dali on Stack Overflow
🌐
Medium
medium.com › @alvaro.saburido › set-theory-for-arrays-in-es6-eb2f20a61848
Array intersection, difference, and union in ES6 | by Alvaro Saburido | Medium
August 2, 2020 - But, there is a problem is that if we use spread operator, we will get elements duplicated, so it’s no theoretically a union. For doing this we have can use a Set to help us out: ... Why? In the article I mentioned earlier, Sets in javascript contains only distinct elements.
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Set › union
Set.prototype.union() - JavaScript | MDN
September 1, 2026 - The union() method of Set instances takes a set and returns a new set containing elements which are in either or both of this set and the given set.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › how-to-compute-union-of-javascript-arrays
JavaScript - Union of Arrays - GeeksforGeeks
We can use the Set object along with the spread operator to find the union of arrays, as Set automatically removes duplicates.
Published: July 12, 2025
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Array › concat
Array.prototype.concat() - JavaScript | MDN
July 10, 2025 - The concat() method of Array instances is used to merge two or more arrays. This method does not change the existing arrays, but instead returns a new array.
🌐
W3Schools
w3schools.com › jsref › jsref_set_union.asp
JavaScript Set union() Method
The union() method returns a new set containing the elements which are in this set, or in the argument set, or in both.
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › javascript-program-to-find-union-and-intersection-of-two-unsorted-arrays
JavaScript Program to Find Union and Intersection of Two Unsorted Arrays - GeeksforGeeks
July 23, 2025 - Creating a function and passing both arrays and their respective lengths. Initializing 2 sets one for storing union and another for storing intersection.
Find elsewhere
🌐
GitHub
gist.github.com › jfollmann › b065d1ebb9043cacfbe9249a572bbd7e
Array intersection, difference, and union in ES6 · GitHub
Array intersection, difference, and union in ES6 · Raw · intersection-difference-union.js · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
Es-toolkit
es-toolkit.dev › reference › array › union.html
union | es-toolkit
const fruits1 = ['apple', 'banana']; const fruits2 = ['banana', 'orange']; union(fruits1, fruits2); // Returns: ['apple', 'banana', 'orange'] Elements from the first array come first, followed by unique elements from the second array.
🌐
QuickRef.ME
quickref.me › home › how to get union of arrays in javascript - quickref.me
How to get union of arrays in JavaScript - QuickRef.ME
In this Article we will go through how to get union of arrays only using single line of code in JavaScript. This is a one-line JavaScript code snippet that uses one of the most popular ES6 features => Arrow Function.
🌐
Substack
imissmyfriendssoimakefood.substack.com › march: greek islands, pot pies, and please dear god the end of quiet luxury
march: greek islands, pot pies, and please dear god the end of quiet luxury
April 6, 2026 - highlighting the very best things to do when the weather turns: visits to the wholesale flower market, union square’s farmer’s market, the brooklyn botanical garden, vintage markets, some of my new favorite spring openings.
🌐
Microsoft Learn
learn.microsoft.com › en-us › azure › logic-apps › expression-functions-reference
Reference for functions in workflow expressions - Azure Logic Apps | Microsoft Learn
July 21, 2026 - To work with collections, generally arrays, strings, and sometimes, dictionaries, you can use these collection functions.
🌐
LeetCode
leetcode.com › problems › intersection-of-two-arrays
Intersection of Two Arrays - LeetCode
Can you solve this real interview question? Intersection of Two Arrays - Given two integer arrays nums1 and nums2, return an array of their intersection. Each element in the result must be unique and you may return the result in any order.
🌐
Zod
zod.dev › api
Defining schemas | Zod
Unlike regular unions that succeed when any option matches, z.xor() fails if zero options match OR if multiple options match.
🌐
freeCodeCamp
forum.freecodecamp.org › javascript
Perform a Union on Two Sets - JavaScript - The freeCodeCamp Forum
July 12, 2018 - My union function is not pass , it print error final.indexOf is not a function !!! Your code so far function Set() { // the var collection will hold the set var collection = []; // this method will check for the presence of an element and return true or false this.has = function(element) { return (collection.indexOf(element) !== -1); }; // this method will return all the values in the set this.values = function() { return collection; }; // t...
Top answer
1 of 10
23

You could implement your own pretty easily. In this case, we make the function generic, so that it can take arrays of any data type(s) and union them using the comparator function provided.

// arr1 and arr2 are arrays of any length; equalityFunc is a function which
// can compare two items and return true if they're equal and false otherwise
function arrayUnion(arr1, arr2, equalityFunc) {
    var union = arr1.concat(arr2);

    for (var i = 0; i < union.length; i++) {
        for (var j = i+1; j < union.length; j++) {
            if (equalityFunc(union[i], union[j])) {
                union.splice(j, 1);
                j--;
            }
        }
    }

    return union;
}

function areGamesEqual(g1, g2) {
    return g1.title === g2.title;
}

// Function call example
arrayUnion(arr1, arr2, areGamesEqual);

Refer to Object comparison in JavaScript for various object comparison implementations.

2 of 10
5

You can do it using the underscore way:

// collectionUnion(*arrays, iteratee)
function collectionUnion() {
    var args = Array.prototype.slice.call(arguments);
    var it = args.pop();

    return _.uniq(_.flatten(args, true), it);
}

It just an improvment of the original function _.union(*arrays), adding an iteratee to work collection (array of object).

Here how to use it:

var result = collectionUnion(a, b, c, function (item) {
    return item.id;
});

The original function which is just working with array, looks like that:

_.union = function() {
  return _.uniq(flatten(arguments, true, true));
};

And in bonus a full example:

// collectionUnion(*arrays, iteratee)
function collectionUnion() {
    var args = Array.prototype.slice.call(arguments);
    var it = args.pop();

    return _.uniq(_.flatten(args, true), it);
}

var a = [{id: 0}, {id: 1}, {id: 2}];
var b = [{id: 2}, {id: 3}];
var c = [{id: 0}, {id: 1}, {id: 2}];

var result = collectionUnion(a, b, c, function (item) {
    return item.id;
});

console.log(result); // [ { id: 0 }, { id: 1 }, { id: 2 }, { id: 3 } ]