JavaScript

How to Push an Object to an Array in JavaScript

In JavaScript, the array is a data structure for storing multiple data, like strings, objects, and so on. In some situations, programmers need to add data at run time in an array. For this purpose, JavaScript offers multiple predefined methods that help push data in the array at any position, the start of the array, the end of the array, or at any specified index.

This post will demonstrate the methods to push/append an object to an array.

How to Push/Add an Object to an Array in JavaScript?

Utilize the given JavaScript predefined methods to push an object to an array:

Method 1: Push an Object to an Array Using push() Method

To push an object to an array, use the “push()” method. It is used to add new elements at the end/last of an array.

Syntax

Use the following syntax to push the object into an array:

array.push(object)

For multiple objects, use the given syntax:

array.push(object1, object2, ...... objectN)

Example 1: Push Multiple Objects to an Empty Array

In the given example, first, create an empty array:

const arr = [];

Now, create three objects “obj1”, “obj2”, and “obj3”:

const obj1 = {name: 'Stephen', id: 15};

const obj2 = {name: 'Robert' ,id: 5};

const obj3 = {name: 'Susan' ,id: 11};

Call the push() method and pass these three objects as an argument to push them in an array:

arr.push(obj1, obj2, obj3);

Lastly, print the array on the console:

console.log(arr);

The output shows that the objects are successfully added to an array:

Example 2: Push a Single Object to an Array

Here, we will see how to push a single object into an array. First, we will create an array of objects:

const arr = [{name: 'Robert' ,id: 5},{name: 'Susan' ,id: 11}];

Create an object that will be added to an array:

const obj1 = {name: 'Stephen', id: 15};

Call the push() method and pass the object to push it in an array:

arr.push(obj1);

Finally, print the array using the “console.log()” method:

console.log(arr);

It can be observed that the pushed object is appended at the end of an array:

Method 2: Push an Object to an Array Using splice() Method

Use the “splice()” method to push an object to an array. The JavaScript splice() method is used to simultaneously add and remove elements from an array or add an object at any index in an array.

Syntax

Follow the given syntax to use the splice() method:

Array.splice(index, removeCount, object)

Here:

  • index” is the position where the element or an object will be added.
  • removeCount” is the number of elements that will be eliminated from an array from the starting index.
  • object” is the object that will be appended to an array.

Example

Call the “splice()” method and pass the index “1”, removeCount “0”, and “obj1” for appending object obj1 at the 1st index of an array by removing zero elements/objects:

arr.splice(1, 0, obj1);

Here, you can see that the obj1 is added at the 1st index by removing any object from an array:

Method 3: Push an Object to an Array Using unshift() Method

Another method to push an object to an array is the “unshift()” method. This JavaScript method will append or push an object or the list of objects at the start of an array.

Syntax

The given syntax is used to add an object at the start of an array:

array.unshift(object)

Example

Call the “unshift()” method and pass the object as a parameter:

arr.unshift(obj1);

The output indicates that the appended object is placed at the beginning of the array:

We have gathered all the best solutions to push an object to an array in JavaScript.

Conclusion

To push an object to an array in JavaScript, use the “push()” method, “splice()” method, or the “unshift()” method. The push() method adds the object at the end of the array, unshift() is used to add the object at the start of an array, and for adding an object at any place in an array, use the splice() method. In this post, we demonstrated the methods to push/add an object to an array.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.