Skip to content
Related Articles
Get the best out of our app
GFG App
Open App
geeksforgeeks
Browser
Continue

Related Articles

How to push an array into the object in JavaScript ?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In order to push an array into the object in JavaScript, we need to utilize the push() function. With the help of Array push function this task is so much easy to achieve. 

push() function: The array push() function 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:  

Javascript




<script>
// JavaScript program to add array into
// an object using push() function
  
// Creating a JS object to add array into it
var Obj = {             
    arrayOne: [],
    arrayTwo: []
};
  
// Array to be inserted
var arraynew = ['Geeks', 'for', 'Geeks'];
  
// Push an array to object
Obj.arrayOne.push(arraynew);     
  
alert(Obj.arrayOne);
      
</script>                     


Output: 

Output after inserting array

Example 2: 

Javascript




<script>
// JavaScript program to add array into
// an object using push() function
  
// Creating a JS object to add array into
var Obj = {             
    arrayOne: ['Geeks', 'for', 'Geeks'],
    arrayTwo: []
};
  
// Array to be inserted
var arraynew = ['Hello', 'World', '!!!']; 
  
// Pushing of array into arrayTwo
Obj['arrayTwo'].push(arraynew);     
  
alert(Obj.arrayTwo);
      
</script>                    


Output: 
 

Output after inserting array2

Supported Browsers: The supported browser by the Array Push() Function are listed below: 

  • Google Chrome 1.0
  • Internet Explorer 5.5
  • Mozilla Firefox 1.0
  • Safari
  • Opera

 

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.


My Personal Notes arrow_drop_up
Last Updated : 26 Jul, 2021
Like Article
Save Article
Similar Reads
Related Tutorials