How to merge two arrays in JavaScript and de-duplicate items

javascript merge two arrays of objects without duplicates es6
javascript merge two objects without duplicates
merge duplicate objects in array javascript
javascript merge array of objects
javascript merge two object arrays without duplicates
js merge array of objects without duplicates
javascript compare two arrays of objects and remove duplicates
javascript merge array of objects by key

I have two JavaScript arrays:

var array1 = ["Vijendra","Singh"];
var array2 = ["Singh", "Shakya"];

I want the output to be:

var array3 = ["Vijendra","Singh","Shakya"];

The output array should have repeated words removed.

How do I merge two arrays in JavaScript so that I get only the unique items from each array in the same order they were inserted into the original arrays?

If you're purely using underscore.js, it doesn't have unionWith, unionBy

you can try out : _.uniq(_.union(arr1, arr2), (obj) => obj.key) ( key is the key param of each object ) this should help to get unique after union of both arrays.

Combining Arrays Without Duplicates ―, Then it creates a set out of that merged array and lastly convers the set and I don't want to mutate existing arrays because of React state stuff. How to Merge Two Arrays in JavaScript and De-duplicate Items. In this tutorial, you will learn how to merge two Arrays in JavaScript. However, the methods depend on the version you’re using. ES5 Version¶ This version uses the concat() method to merge two arrays:

You can use loadash unionWith - _.unionWith([arrays], [comparator])

This method is like _.union except that it accepts comparator which is invoked to compare elements of arrays. Result values are chosen from the first array in which the value occurs. The comparator is invoked with two arguments: (arrVal, othVal).

var array1 = ["Vijendra","Singh"];
var array2 = ["Singh", "Shakya"];
var array3 = _.unionWith(array1, array2, _.isEqual);
<script src=""></script>

JavaScript array: Merge two arrays and removes all duplicates , How to merge two arrays in Javascript and de-duplicate items ? Solution 1: Here We can use a greedy approach to go through each array and track the smallest  The easiest way to do this is either to use concat() to merge the arrays and then use filter() to remove the duplicates, or to use concat() and then put the merged array inside a Set().


You can use it like union(array1, array2, array3, ...)

 * Merges two or more arrays keeping unique items. This method does
 * not change the existing arrays, but instead returns a new array.
function union<T>(...arrays: T[]) {
  return [ Set([...arrays].flat())];

It is ES2019 because of the flat() function, but you can use core-js to get it as a polyfill. The T here is TypeScript generic type, that you can remove if you are not using TypeScript. If you are using TypeScript, make sure to add "lib": ["es2019.array"] to compiler options in tsconfig.json.


just use lodash _.union

Merge two arrays without duplicates in JavaScript, In ES5 you can merge two Arrays using the .concat() method. var fruits There are two important things to note about how .concat() works: If you'd like to also remove duplicates, you can do so using the .filter() method. Spread the love Related Posts Cloning Arrays in JavaScriptThere are a few ways to clone an array in JavaScript, Object.assign Object.assign allows us… How to Merge Arrays in JavaScriptOftentimes, we need to combine multiple arrays into one array. With multiple arrays, combining them… Append Item to a JavaScript ArrayAppending an item to an array in …

I think this works faster.

removeDup = a => {

    for (let i = a.length - 1; i >= 0; i--) {
        for (let j = i-1; j >= 0; j--) {
            if (a[i] === a[j])
                a.splice(j--, 1);

    return a;

[Solved-100% Working Code], merge two arrays and remove duplicate in es6. GitHub Gist: instantly share code, notes, and bambooom/demo.js. Created 4 years ago. Star 6 · Fork 2. See the Pen JavaScript - Merge two arrays and removes all duplicates elements - array-ex- 30 by w3resource (@w3resource) on CodePen. Improve this sample solution and post your code through Disqus. Previous: Write a JavaScript function to fill an array with values (numeric, string with one character) on supplied bounds.

   //1.merge two array into one array

   var arr1 = [0, 1, 2, 4];
   var arr2 = [4, 5, 6];

   //for merge array we use "Array.concat"

   let combineArray = arr1.concat(arr2); //output

   alert(combineArray); //now out put is 0,1,2,4,4,5,6 but 4 reapeat

   //2.same thing with "Spread Syntex"

   let spreadArray = [...arr1, ...arr2];

   alert(spreadArray);  //now out put is 0,1,2,4,4,5,6 but 4 reapete

       if we need remove duplicate element method use are
       1.Using set
       2.using .filter
       3.using .reduce

JavaScript Bits: How to merge two Arrays?, JavaScript O(N) 131 124 116 92 (86?) Having said all that, if I were asked to merge two arrays arr1 and arr2 such that the result Could be 21, if overwriting global values is allowed (it would remove my from the code). Here are 2 ways to combine your arrays and return a NEW array. I like using the Spread operator. But if you need older browser support, you should use Concat. Alternatively, you can also write the concat method, in this regard: As you can see, this way of writing it doesn't manipulate or change the existing array. Which one should I pick?

merge two arrays and remove duplicate in es6 · GitHub, csharp #linq #dotnet #dotnetcore #webdeveloper #Coding #javascript #js #​developers Duration: 2:09 Posted: Nov 29, 2018 I'm trying to combine 2 arrays in javascript into one. How to join / combine two arrays to concatenate into one array? How to merge two arrays in JavaScript

Array Merge without Duplicates, In the second step, we remove duplicate elements from mergedArray . Already, we have discussed two methods to remove duplicate elements  The concat() method is used to join two or more arrays. This method does not change the existing arrays, but returns a new array, containing the values of the joined arrays.

How to merge two arrays in JavaScript and de-duplicate items , In order to merge two arrays, we find its length and stored in fal and sal variable respectively. After that, we create a new integer array result which stores the sum of length of both arrays. Now, copy each elements of both arrays to the result array by using arraycopy() function.

  • This doesn't merge arrays—this just overwrites one with another. E.g., var f = [1]; f.merge([2]);
  • Hmmm... but I thought the keys that are already there stay, and you want the new ones and the merged one to overwrite? Oh maybe I was thinking dictionaries or something.
  • What does this do that's different?
  • I wrote it with performance in mind. There are many ways to do what the OP wants, but performance is usually overlooked. You can check it out link for an updated one, three years later. @djechlin
  • Thank you so much. In synchronous jQuery operations this was that variation that worked for me for inline generated arrays.
  • What's different about this from the accepted answer and several other ~20 lines of raw JS answers?
  • @djechlin it's different because, as I said in the description, it takes "sorted arrays of simple types" and it is pretty good because it is portable and O(n) which is faster than answers O(n^2). but no worries. i don't object to the down-vote!
  • Of coruse if you want to return an array then just use Array.from(merge(arguments))
  • what browsers support concat?
  • @chovy - concat is an old method, from ECMAScript 3. You shouldn't have any problem using it. See the compatibility table on the MDN.
  • Does not meet de-duplicate criterion
  • This answer was copied from…