How to Merge Two Arrays in JavaScript and De-duplicate Items
Read this tutorial and find useful information about the simplest methods that are used for merging two arrays and removing duplicate items in JavaScript.
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:
Javascript array concat
The concat() method merges two or more arrays not changing the existing arrays, but returns a new array.
To remove duplicates, the filter() method can be used:
Javascript array concat
ES6 Version
This version uses the destructuring method for merging two <kbd class="highlighted">Arrays</kbd>.
Javascript merging two arrays
The push() method is an alternate way to merge two <kbd class="highlighted">Arrays</kbd>.
Javascript push method merging two arrays
To remove duplicates, Set is used. The Set object is used to store unique values of primitive values or object references.
Javascript push method merging two arrays
Arrays
The JavaScript <kbd class="highlighted">Arrays</kbd> class is a global object used in the construction of arrays, which are high-level and list-like objects. You can use arrays for storing several values in a single variable. An array can be described as a unique variable that is capable of holding more than one value simultaneously.