Open In App

JavaScript- Add an Object to JS Array

Last Updated : 10 May, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In JavaScript, arrays are used to store multiple values in a single variable, and objects are collections of properties and values. Sometimes, we may need to add an object to an array to manage data more effectively.

These are the following ways to add an object to a JS array:

  • Using JavaScript Array push() Method
  • Using JavaScript Array unshift() Method
  • Using Array Concatenation (concat)
  • Using Spread Operator

1. Using JavaScript Array push() Method

The push() method is used to add one or multiple elements to the end of an array. It returns the new length of the array formed. An object can be inserted by passing the object as a parameter to this method.

JavaScript
let obj = { name: "GFG", age: 30 };
let arr = ["Emma", "Sophia"];
arr.push(obj);
console.log(arr);

Output
[ 'Emma', 'Sophia', { name: 'GFG', age: 30 } ]

In this example

  • let obj = { name: "GFG", age: 30 };: Defines an object obj with properties name and age.
  • let arr = ["Emma", "Sophia"];: Defines an array arr with two string elements.
  • arr.push(obj);: Adds the object obj to the end of the array arr.
  • console.log(arr);: Logs the updated array to the console

2. Using JavaScript Array unshift() Method

The unshift() method is used to add one beginning of an array. It returns the length of the new array formed. An object can be inserted by passing the object as a parameter to this method.

JavaScript
let list = ["GFG", "Contribute", "Explore"];
let obj = { name: "GFG User", age: 30 };
list.unshift(obj);
console.log(list);

Output
[ { name: 'GFG User', age: 30 }, 'GFG', 'Contribute', 'Explore' ]

In this example

  • let list = ["GFG", "Contribute", "Explore"]; – Defines an array list with three string elements.
  • let obj = { name: "GFG User", age: 30 }; – Defines an object obj with properties name and age.
  • list.unshift(obj); – Adds the object obj to the beginning of the array list.
  • console.log(list); – Logs the updated array to the console.

3. Using Array Concatenation (concat)

This approach involves using the concat method, which combines two arrays non-destructively. It returns a new array containing all elements from the original arrays without modifying them.

JavaScript
let list = ["Python", "C#", "GO"];
let obj = { name: "Java", usedBy: "3000000+" };
const newArray = list.concat(obj);
console.log(newArray);

Output
[ 'Python', 'C#', 'GO', { name: 'Java', usedBy: '3000000+' } ]

In this example

  • let list = ["Python", "C#", "GO"]; – Defines an array list with three string elements.
  • let obj = { name: "Java", usedBy: "3000000+" }; – Defines an object obj with properties name and usedBy.
  • const newArray = list.concat(obj); – Combines the array list and the object obj into a new array newArray. The object is added as a single element.

4. Using Spread Operator

The spread operator (...) is used to expand elements of an iterable (like an array) into places where multiple elements are expected. We can utilize the spread operator to add an object to an array.

JavaScript
let obj = { name: "GFG User", age: 30 };
let arr = ["Emma", "Sophia"];
arr = [...arr, obj];
console.log(arr);

Output
[ 'Emma', 'Sophia', { name: 'GFG User', age: 30 } ]

In this example

  • let obj = { name: "GFG User", age: 30 }; – Defines an object obj.
  • let arr = ["Emma", "Sophia"]; – Defines an array arr.
  • arr = [...arr, obj]; – The spread operator (...) creates a new array by copying all elements of arr and appending the object obj at the end.
  • console.log(arr); – Logs the updated array.

5. Using splice() Method

The splice() method is more flexible and allows you to add an object to an array at a specific index. It can be used to add, remove, or replace elements in an array.

JavaScript
let users = [
    { name: "Jiya", age: 30 },
    { name: "Jolly", age: 25 }
];

let newUser = { name: "Meeta", age: 35 };
users.splice(1, 0, newUser);  
console.log(users);

Output

[
{ name: "Jiya", age: 30 },
{ name: "Meeta", age: 35 },
{ name: "Jolly", age: 25 }
]

In this example

  • The splice() method inserts the object { name: "Meeta", age: 35 } at index 1, which is between "Jiya" and "Jolly".
  • The second argument is 0, meaning no elements are removed, just the object is added.

Conclusion

In JavaScript, we can add objects to arrays using various methods. The push() method adds objects to the end, unshift() adds to the beginning, and concat() combines arrays. The spread operator offers a modern way to append objects, while splice() allows insertion at specific indices. Each method provides flexibility to manage arrays based on your needs.


Next Article

Similar Reads