Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
September 15, 2022 10:16 am GMT

How to merge two arrays?

Lets say you have two arrays and want to merge them:

const firstTeam = ['Olivia', 'Emma', 'Mia']const secondTeam = ['Oliver', 'Liam', 'Noah']

One way to merge two arrays is to use concat() to concatenate the two arrays:

const total = firstTeam.concat(secondTeam)

But since the 2015 Edition of the ECMAScript now you can also use spread to unpack the arrays into a new array:

const total = [...firstTeam, ...secondTeam]console.log(total)

The result would be:

// ['Olivia', 'Emma', 'Mia', 'Oliver', 'Liam', 'Noah']

There is also another way, in case you don't want to create a new array but modify one of the existing arrays:

firstTeam.push(...secondTeam);firstTeam; // ['Olivia', 'Emma', 'Mia', 'Oliver', 'Liam', 'Noah']

Original Link: https://dev.to/jolamemushaj/how-to-merge-two-arrays-3eb0

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To