Hello Friends đź‘‹,

Welcome To Infinitbility! ❤️

In this article, you will learn the Best practice to merge arrays in javascript or merge arrays without duplicate values.

Let’s Start today topic Merge arrays in javascript

Javascript concat function

Javascript provide concat function to merge arrays let’s undertand with Merge two arrays example.

let array1 = [1, 2, 3];
let array2 = [2, 3, 4];

let array3 = array1.concat(array2);
console.log(array3);

/*
 *  Output
 *  [1, 2, 3, 2, 3, 4]
 */

Javascript Merge without dublicate values

Using JavaScript concat we know it will merge same value also but you need only unique values show or merge in third array.

Let’s undertand with Merge arrays in javascript without dublication example.


let array1 = [1, 2, 3];
let array2 = [2, 3, 4];

let array3 = [...new Set([...array1,...array2])]
console.log(array3);

/*
 *  Output
 *  [1, 2, 3, 4]
 */

Thanks For Reading…