DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • React’s Unstoppable Rise: Why It’s Here to Stay
  • Angular RxJS Unleashed: Supercharge Your App With Reactive Operators
  • TypeScript: Useful Features
  • Java String: A Complete Guide With Examples

Trending

  • Ethical AI in Agile
  • From Zero to Production: Best Practices for Scaling LLMs in the Enterprise
  • Beyond ChatGPT, AI Reasoning 2.0: Engineering AI Models With Human-Like Reasoning
  • How to Practice TDD With Kotlin
  1. DZone
  2. Data Engineering
  3. Data
  4. Three Ways to Combine Arrays in JavaScript

Three Ways to Combine Arrays in JavaScript

Easily and efficiently combine arrays in JavaScript.

By 
Ashutosh Singh user avatar
Ashutosh Singh
DZone Core CORE ·
Updated Jul. 26, 19 · Tutorial
Likes (5)
Comment
Save
Tweet
Share
141.9K Views

Join the DZone community and get the full member experience.

Join For Free

1. Concat()

The most basic way is using the concat() method. It's very simple; you just have to define two arrays and combine them as shown. (Remember the sequence matters.)

let firstArray = [1,2,3,'Shinchan']
let secondArray = ['Nohara',4,5,6]
let combinedArray1 = firstArray.concat(secondArray)
let combinedArray2 = secondArray.concat(firstArray)

console.log(`Combined Array1 = `+combinedArray1)
// Combined Array1 = 1,2,3,Shinchan,Nohara,4,5,6

console.log(`Combined Array2 = `+combinedArray2)
//Combined Array2= Nohara,4,5,6,1,2,3,Shinchan


2. Using a Spread Operator (ES6 Shortcut)

Now the second method is like a shortcut; you just have to store the values of two or more arrays in a different array using ellipses or spread operator. This is only available in the ES6 version. It's my favorite way to combine arrays, as it's very efficient.

As you can see, sequence matters here too.

let firstArray = [1, 2, 3, 'Shinchan']
let secondArray = ['Nohara', 4, 5, 6]
let thirdArray = [7, 8, 9, 'Himawari']

let combinedArray1 = [...firstArray, ...secondArray, ...thirdArray]
let combinedArray2 = [...secondArray, ...firstArray, ...thirdArray]

console.log('CombinedArray1 = ' + combinedArray1)
//Combined Array1 = 1,2,3,Shinchan,Nohara,4,5,6,7,8,9,Himawari

console.log('CombinedArray2 = ' + combinedArray2)
//Combined Array2 = Nohara,4,5,6,1,2,3,Shinchan,7,8,9,Himawari


3. Merge Arrays With Push

You can also use the push method to combine different arrays like as shown.

let firstArray = [1, 2, 3, 'Shinchan']
let secondArray = ['Nohara', 4, 5, 6]
let thirdArray = [7, 8, 9, 'Himawari']

let combinedArray = []

combinedArray.push(...firstArray, ...secondArray, ...thirdArray)

console.log(`CombinedArray = `+combinedArray)
// CombinedArray = [1,2,3,'Shinchan','Nohara',4,5,6,7,8,9,'Himawari']


First, declare an empty array and then use the push() method with the spread operator. The contents of the arrays will be copied in the desired array.

Remember, if you don't use the spread operator, then the whole array will be pushed and you will get a nested array called a two-dimensional array. (That is for another day’s discussion, so let’snot get into that.)

let firstArray = [1, 2, 3, 'Shinchan']
let secondArray = ['Nohara', 4, 5, 6]
let thirdArray = [7, 8, 9, 'Himawari']


let combinedArray = []


combinedArray.push( firstArray, secondArray, thirdArray)

console.log(`CombinedArray = `+combinedArray)
/* CombinedArray = [ [1,2,3,'Shinchan'],
                     ['Nohara',4,5,6],
                     [7,8,9,'Himawari'] ]


That’s all. If you think there are any errors, or you want to provide some information feel free to comment below

Data structure JavaScript Operator (extension) IT push Merge (version control)

Published at DZone with permission of Ashutosh Singh, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • React’s Unstoppable Rise: Why It’s Here to Stay
  • Angular RxJS Unleashed: Supercharge Your App With Reactive Operators
  • TypeScript: Useful Features
  • Java String: A Complete Guide With Examples

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!