Waiting Answer November 05, 2023

How to Merge Two Arrays in JavaScript?

Answers
2024-01-25 12:45:01

We can merge two arrays in JavaScript using the concat() method. Here’s an example:

const array1 = [1, 2, 3];
const array2 = [4, 5, 6];
const mergedArray = array1.concat(array2);

 

This will create a new array mergedArray that contains all the elements of array1 and array2 in the same order as they were inserted into the original arrays .

If you want to remove duplicates from the merged array, you can use the Set object. Here’s an example:

const array1 = [1, 2, 3];
const array2 = [2, 3, 4];
const mergedArray = [...new Set([...array1, ...array2])];

 

This will create a new array mergedArray that contains all the unique elements of array1 and array2 in the same order as they were inserted into the original arrays.

 

Your Answer

Recently Asked Questions

PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language.PHP is a server-side scripting language designed for web development but also used as a general-purpose programming language.

Python has a variety of libraries such as NumPy, pandas, and matplotlib that make it an ideal language for data analysis and visualization.

Java is commonly used for building enterprise-scale applications.