In this shot, we will learn how to merge two arrays and remove duplicates from them using JavaScript.
We will observe the following steps:
concat() method.set out of the merged array, removing duplicates.Let's take a look at an example.
// given first arrayconst fruits1 = ["apples", "oranges"];//given second arrayconst fruits2 = ["oranges", "grapes", "guava"];//display the merged array after removing duplicatesconsole.log(Array.from(new Set(fruits1.concat(fruits2))));
In the above code snippet:
fruits1.fruits2.concat() method on the first array fruits1 and pass the second array fruits2 as a second parameter. This will return the merged array. Next, we will pass this merged array to a set, which removes duplicates from it. Now, we will pass this set to array and display the returned array.