JavaScript array merging case study

JavaScript array merging case study

Method 1:

var a = [1,2,3];
var b=[4,5]
a = a.concat(b);
console.log(a);
//The output here is [1, 2, 3, 4, 5]

Method 2:

// ES5 notation var arr1 = [0, 1, 2];
var arr2 = [3, 4, 5];
Array.prototype.push.apply(arr1, arr2);
console.log(arr1) // [0, 1, 2, 3, 4, 5]

Method 3:

// ES6 way var arr1 = [0, 1, 2];
var arr2 = [3, 4, 5];
arr1.push(...arr2);
console.log(arr1) // [0, 1, 2, 3, 4, 5]
// ES6 way var arr1 = [0, 1, 2];
var arr2 = [3, 4, 5];
var arr3=[...arr1, ...arr2]
console.log(arr3) // [0, 1, 2, 3, 4, 5]

This is the end of this article about JavaScript array merging cases. For more relevant JavaScript array merging content, please search for previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope everyone will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • Commonly used JavaScript array methods
  • JavaScript Array Detailed Summary
  • Common array operations in JavaScript
  • JavaScript Array Methods - Systematic Summary and Detailed Explanation
  • Summary of examples of common methods of JavaScript arrays
  • JS uses map to integrate double arrays
  • Detailed explanation of built-in methods of javascript array

<<:  Advanced and summary of commonly used sql statements in MySQL database

>>:  Practical way to build selenium grid distributed environment with docker

Recommend

CSS implementation code for drawing triangles (border method)

1. Implement a simple triangle Using the border i...

Document Object Model (DOM) in JavaScript

Table of contents 1. What is DOM 2. Select elemen...

Example code for CSS to achieve horizontal lines on both sides of the text

This article introduces the sample code of CSS to...

How MySQL supports billions of traffic

Table of contents 1 Master-slave read-write separ...

How to create a database in navicat 8 for mysql

When developing a website, you often need to use ...

Implementation of whack-a-mole game in JavaScript

This article shares the specific code for JavaScr...

Teach you how to achieve vertical centering elegantly (recommended)

Preface There are many ways to center horizontall...

Detailed example of SpringBoot+nginx to achieve resource upload function

Recently, I have been learning to use nginx to pl...

JavaScript implements an input box component

This article example shares the specific code for...

Vue implements scroll loading table

Table of contents Achieve results Rolling load kn...

jQuery plugin to implement accordion secondary menu

This article uses a jQuery plug-in to create an a...

js to achieve 3D carousel effect

This article shares the specific code for impleme...