SoFunction
Updated on 2025-03-01

Summary of 5 ways to add specified elements in an array in JavaScript

Preface

In modern web development, JavaScript is an indispensable programming language. It has powerful features and flexible syntax, allowing developers to handle various tasks easily. Among them, processing arrays is one of the common operations in JavaScript.

When working with arrays, we often need to add specific elements to the array. This may be because we need to sort, filter, map or other operations on the array. To this end, JavaScript provides several ways to achieve this.

This article will introduce five methods to add specified elements in an array in JavaScript. These methods are using push() function, using concat() function, using splice() function, using extension operator (...), and using array index assignment. Each method has its applicable scenarios and features, and developers can choose the appropriate method according to specific needs.

Through this article, readers will learn how to use these five methods to increase the specified elements in an array and be able to flexibly use them according to actual needs. This will help improve the efficiency and readability of the code, thereby improving development effectiveness.

Both beginners and experienced developers can make a huge profit from this article. Let's dive into five ways to add specified elements in arrays in JavaScript!

1. What is an array?

An array is a data structure that stores multiple values ​​of the same type. It is one of the very important data types in JavaScript. An array can contain any number of elements, and each element can be accessed through an index.

In JavaScript, an array can contain elements of any type, including numbers, strings, objects, etc. The length of the array is dynamic and elements can be dynamically increased or decreased as needed.

The index of the array starts at 0, indicating the first element. You can use indexes to access elements in an array, or you can use indexes to modify or delete elements. By using the built-in method of arrays, you can perform various operations on the array, such as adding elements, deleting elements, sorting arrays, etc.

Arrays are very common in programming, especially when processing collection data. It provides a convenient and quick way to organize and manipulate multiple values, allowing developers to process data more efficiently.

2. Array addition steps

When you need to add a specified element to a JavaScript array, you can use the following five ES6-based methods:

Method 1: Use the Spread operator The Spread operator can expand an array into multiple elements and add new elements during the expansion. Code example:

const array = [1, 2, 3];
const newArray = [...array, 4];
(newArray); // Output [1, 2, 3, 4]

Method 2: Use the() method to convert an array object or iterable object into a new array, and new elements can be added during the conversion process. Code example:

const array = [1, 2, 3];
const newArray = (array).concat(4);
(newArray); // Output [1, 2, 3, 4]

Method 3: Use the () push() method to add one or more elements at the end of the array. Code example:

const array = [1, 2, 3];
(4);
(array); // Output [1, 2, 3, 4]

Method 4: Use the () unshift() method to add one or more elements at the beginning of an array. Code example:

const array = [1, 2, 3];
(0);
(array); // Output [0, 1, 2, 3]

Method 5: Use the () splice() method to insert one or more elements at a specified location. Code example:

const array = [1, 2, 3];
(1, 0, 1.5);
(array); // Output [1, 1.5, 2, 3]

Summarize

In JavaScript, there are several ways to add specified elements to an array. The following is a summary of 5 commonly used methods based on ES6:

  • Use the Spread operator (...): Expand the original array into multiple elements and add new elements during the expansion.
const array = [1, 2, 3];
const newArray = [...array, 4];
  • Use the() method: Convert a class array object or iterable object to an array and add new elements during the conversion process.
const array = [1, 2, 3];
const newArray = (array).concat(4);
  • Use the() method: Add one or more elements at the end of the array.
const array = [1, 2, 3];
(4);
  • Use the() method: Add one or more elements at the beginning of the array.
const array = [1, 2, 3];
(0);
  • Use the() method: Insert one or more elements at the specified position.
const array = [1, 2, 3];
(1, 0, 1.5);

These methods provide flexible options to select appropriate methods as needed to add specified elements to the array. Whether adding elements at the beginning, end or in the middle, JavaScript provides convenient ways to operate.

Attachment: How to add objects to specific positions in the array

In JavaScript, we can add an object to a specific location in the array using the following method:

  • Use the splice() method: the splice() method can insert an object at the specified index position and optionally delete elements in the array. Its syntax is as follows: (index, 0, item); where index represents the location of the object to be inserted, the second parameter 0 represents no elements deleted, and item represents the object to be inserted.

  • Use the splice() method to add multiple objects: If you want to add multiple objects at once, you can pass them as parameters to the splice() method. For example: (index, 0, item1, item2, ...);

Here is a sample code that demonstrates how to insert objects into specific locations in an array:

var fruits = ["apple", "banana", "orange"];
var newFruit = "pear"; // Object to be insertedvar insertIndex = 1; // Index position to be inserted
(insertIndex, 0, newFruit);

(fruits); // Output: ["apple", "pear", "banana", "orange"]

In the example above, we insert the "pear" object into the index position 1 of the fruits array, i.e. before "banana".

It should be noted that the splice() method will modify the original array. If you want to create a new array instead of modifying the original array, you can use the slice() method to copy the original array into the new array and then operate on the new array.

This is the article about 5 methods to add specified elements in arrays in JavaScript. For more related js, please search for my previous articles or continue browsing the related articles below. I hope you support me in the future!