A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/javascript/how-to-push-an-array-into-the-object-in-javascript/ below:

How to Push an Array into Object in JavaScript?

How to Push an Array into Object in JavaScript?

Last Updated : 12 Jul, 2025

To push an array into the Object in JavaScript, we will be using the JavaScript Array push() method. First, ensure that the object contains a property to hold the array data. Then use the push function to add the new array in the object.

Understanding the push() Method

The array push() method adds one or more values to the end of the array and returns the new length. This method changes the length of the array. But here we will use this function to push the whole array into an object.

Syntax

arr.push(element1[, ...[, elementN]])

An array can be inserted into the object with push() function, below examples illustrate the above approach:

Example 1: This example demonstrates adding an array into the object using the push method.

JavaScript
// Creating a JS object to add array into it
let Obj = {             
    arrayOne: [],
    arrayTwo: []
};

// Array to be inserted
let arraynew = ['Geeks', 'for', 'Geeks'];

// Push an array to object
Obj.arrayOne.push(arraynew);     

console.log(Obj.arrayOne);

Output
[ [ 'Geeks', 'for', 'Geeks' ] ]

We can also push the array elements inside the object instead of pushing the complete array with the help of spread operator.

Example 2: This example adds multiple elements to the object’s array property using the push() function.

JavaScript
// Creating a JS object to add array into it
let Obj = {             
    arrayOne: [],
    arrayTwo: []
};

// Array to be inserted
let arraynew = ['Geeks', 'for', 'Geeks'];

// Push an array items to object
Obj.arrayOne.push(...arraynew);     

console.log(Obj.arrayOne);

Output
[ 'Geeks', 'for', 'Geeks' ]

Example 3: You can also add multiple arrays or elements to the object’s array property using the push() function.

JavaScript
// Creating a JS object to add array into
let Obj = {             
    arrayOne: ['Geeks', 'for', 'Geeks'],
    arrayTwo: []
};

// Array to be inserted
let arraynew = ['Hello', 'World', '!!!']; 

// Pushing of array into arrayTwo
Obj['arrayTwo'].push(arraynew);     

console.log(Obj.arrayTwo);

Output
[ [ 'Hello', 'World', '!!!' ] ]
Supported Browsers

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4