The Wayback Machine - https://web.archive.org/web/20240828171400/https://www.geeksforgeeks.org/how-to-push-an-array-into-the-object-in-javascript/
Open In App

How to push an array into the object in JavaScript ?

Last Updated : 26 Jul, 2021
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.



Previous Article
Next Article

Similar Reads

How to Push an Object into Another Object in JavaScript ?
In JavaScript, the concept of pushing an object into another object involves embedding one object within another as a property. This technique is useful for organizing data structures where related information is grouped, enhancing the ability to manage and manipulate complex data sets effectively. There are several ways to push an object into anot
4 min read
How to Push an Object into an Array using For Loop in JavaScript ?
JavaScript allows us to push an object into an array using a for-loop. This process consists of iterating over the sequence of values or indices using the for-loop and using an array manipulation method like push(), to append new elements to the array. We have given an empty array, and we need to push the object into the array. Below is an example
3 min read
Difference Between Git Push Origin and Git Push Origin Master
Understanding the difference between git push origin and git push origin master is important for efficient version control in Git. These commands are used to upload changes from your local repository to a remote repository, but they function differently. This article will explain these differences, how each command works, and when to use them. What
3 min read
How to push data into firebase Realtime Database using ReactJS ?
Firebase is a popular backend-as-a-service platform that provides various services for building web and mobile applications. One of its key features is the Realtime Database, which allows developers to store and sync data in real-time. In this article, we will explore how to push data into the Firebase Realtime Database using ReactJS. The following
2 min read
How to Search Character in List of Array Object Inside an Array Object in JavaScript ?
Searching for a character in a list of array objects inside an array object involves inspecting each array object, and then examining each array within for the presence of the specified character. The goal is to determine if the character exists within any of the nested arrays. There are several ways to search for characters in a list of array obje
4 min read
JavaScript Array push() Method
The push() method in JavaScript adds one or more elements to the end of an array and returns the new length of the array. It modifies the original array, increasing its length by the number of elements added. Syntax arr.push(element0, element1, â€Ķ , elementN);Parameters This method contains as many numbers of parameters as the number of elements to
3 min read
How Check if object value exists not add a new object to array using JavaScript ?
In this tutorial, we need to check whether an object exists within a JavaScript array of objects and if they are not present then we need to add a new object to the array. We can say, every variable is an object, in Javascript. For example, if we have an array of objects like the following. obj = { id: 1, name: ''Geeks1" }, { id: 2, name: '"Geeks2"
3 min read
How to Add Duplicate Object Key with Different Value to Another Object in an Array in JavaScript ?
Adding duplicate object keys with different values to another object in an array in JavaScript refers to aggregating values under the same key from multiple objects in an array, creating a new object where each key corresponds to an array of associated values. Table of Content Using for...of LoopUsing reduce()Using a Map for Multiple ValuesUsing fo
4 min read
How to convert arguments object into an array in JavaScript ?
The arguments object is an array-like object that represents the arguments passed in when invoking a function. This array-like object does not have the array prototype chain, hence it cannot use any of the array methods. This object can be converted into a proper array using two approaches: Method 1: Using the Array.from() method: The Array.from()
5 min read
How to convert two-dimensional array into an object in JavaScript ?
In this article, we will learn how to convert a two-dimensional array to an object. A two-dimensional array can have any number of rows and two columns. Example: Input: [ ["John", 12], ["Jack", 13], ["Matt", 14], ["Maxx", 15] ] Output: { "John": 12, "Jack": 13, "Matt": 14, "Maxx": 15 }The below approaches can be followed to solve the problem. Appro
4 min read
How to Convert an Object into Array of Objects in JavaScript?
Converting an object into an array of objects can be done by converting the properties and values of the object into individual objects within an array. Below are the approaches to converting an object into an Array of objects using JavaScript: Table of Content Using Object.values() methodUsing Object.keys() methodUsing Object.entries() methodUsing
4 min read
Alternatives of push() method in JavaScript
The task is to perform a push operation without using the push() method with the help of JavaScript. There are two approaches that are discussed below. Approach 1: Use the length property to insert the element at the end of the array. Example: This example implements the above approach. C/C++ Code &lt;body style=&quot;text-align: center;&quot;&gt;
2 min read
What is the difference between unshift() and Push() method in JavaScript?
JavaScript Unshift() method is very much similar to the push() method but the difference is that the unshift() method adds the elements at the very beginning of the array whereas the push() method adds at the end of the array. Javascript Array unshift() method: The JavaScript Array unshift() Method is used to add one or more elements to the beginni
2 min read
What is the use of the push() method in JavaScript Arrays ?
In JavaScript, the push() method is used to add one or more elements to the end of an array. It modifies the original array by appending the specified values. This can be particularly useful when you want to dynamically expand the size of an array or simply add new elements to the existing list. Example: Here, we are pushing newElement in myArray.
1 min read
How to push elements in an array using AngularJS ?
Given an array and the task is to perform the push operation on the array using AngularJS. The arr.push() method is used to push one or more values into the array, &amp; new values will add at the end of an array. This method changes the length of the array by the number of elements added to the array &amp; returns the new length of the modified ar
2 min read
How to Push Item From an Array in Mongoose ?
In Mongoose, pushing an item to an array can be done using different approaches. To push an item into an array, you can use the $push operator along with the updateOne() or updateMany() method. We will discuss the different methods to push items from an array in Mongoose: Table of Content Inserting a new document Using $push OperatorUsing addToSet
4 min read
How to Push Data to Array Asynchronously & Save It in Node.js ?
Node.js is a powerful environment for building scalable and efficient applications, and handling asynchronous operations is a key part of its design. A common requirement is to collect data asynchronously and store it in an array, followed by saving this data persistently, for instance, in a database or a file. This article explores the techniques
6 min read
TypeScript Array push() Method
The Array.push() method in TypeScript is a built-in function used to append one or more elements to the end of an array. It modifies the original array and returns the new length of the array. SyntaxHere is the syntax for using the push() method: array.push(element1, ..., elementN)Parameter: This method accept a single parameter as mentioned above
2 min read
How to check a JavaScript Object is a DOM Object ?
Prerequisite: DOM (Document object model), Instanceof Operator DOM (Document Object Model): Document Object Model is a hierarchical representation of HTML and XML documents in a format that is easier to interpret in terms of programming. It makes manipulation of tags, elements, attributes, and classes by interpreting its structure in the form of a
2 min read
How to access an object having spaces in the object's key using JavaScript ?
Given an object and the task is to access the object in which the key contains spaces. There are a few methods to solve this problem which are discussed below: Approach 1: Create an object having space-separated keys.Use square bracket notation instead of dot notation to access the property. Example: This example implements the above approach. C/C+
2 min read
How to check if the provided value is an object created by the Object constructor in JavaScript ?
In this article, we will learn how to check if the provided value is an object created by the Object constructor in JavaScript. Almost all the values in JavaScript are objects except primitive values. There are several methods that can be used to check if the provided value is an object created by the Object constructor in JavaScript. Using the ins
3 min read
How to create a new object from the specified object, where all the keys are in lowercase in JavaScript?
In this article, we will learn how to make a new object from the specified object where all the keys are in lowercase using JavaScript. Example: Here, we have converted the upper case to lower case key values. Input: {RollNo : 1, Mark : 78} Output: {rollno : 1, mark : 78} Approach 1: A simple approach is to Extract keys from Object and LowerCase to
2 min read
Difference between Object.keys() and Object.entries() methods in JavaScript
Object.keys() and Object.entries() are methods in JavaScript used to iterate over the properties of an object. They differ in how they provide access to object properties: Object.keys() returns an array of a given object's own enumerable property names, while Object.entries() returns an array of a given object's own enumerable string-keyed property
2 min read
How to compare two objects to determine the first object contains equivalent property values to the second object in JavaScript ?
In this article, we are going to learn about comparing two objects to determine if the first object contains equivalent property values to the second object, In JavaScript, comparing the values of two objects involves checking if they have the same properties with corresponding values. Given two objects obj1 and obj2 and the task are to check that
6 min read
How to Append an Object as a Key Value in an Existing Object in JavaScript ?
In JavaScript, An object is a key-value pair structure. The key represents the property of the object and the value represents the associated value of the property. In JavaScript objects, we can also append a new object as a Key-value pair in an existing object in various ways which are as follows. Table of Content Using JavaScript Spread (...) Ope
3 min read
Difference Between Object.keys() and Object.getOwnPropertyNames() in JavaScript
In JavaScript, Object.keys() and Object.getOwnPropertyNames() both retrieve properties of an object but differ in scope. Object.keys() returns an array of an object's own enumerable property names. In contrast, Object.getOwnPropertyNames() returns an array of all own property names, including non-enumerable properties. These methods are useful for
3 min read
How to convert rgb() color string into an object in JavaScript ?
Given a color in the form of rgb( ) or rgba() and the task is to convert it into an object where the keys are the color names and values are the color values. Example:Input: rgb(242, 52, 110) Output: { red: 242, green: 52, blue: 110 } Input: rgba(255, 99, 71, 0.5) Output: { red: 255, green: 99, blue: 71, alpha: 0.5 } Approach: To achieve this we us
2 min read
How to serialize an object into a list of URL query parameters using JavaScript ?
Given a JavaScript Object and the task is to serialize it into a URL query parameters using JavaScript. Approach 1: Declare an object and store it in the variable.Then use JSON.stringify() method to convert a JavaScript object into strings and display the content.Next, take an empty string and append (key, value) pairs of objects to it by accessing
2 min read
Safely Turning a JSON String into an Object in JavaScript
JSON (JavaScript Object Notation) is a data format used for storing and exchanging data. It is a lightweight and flexible format that can be easily parsed and understood by both humans and machines. When working with JSON data, it is important to know how to safely turn a JSON string into an object. In this article, we will discuss the steps you ca
5 min read
How to change JSON String into an Object in JavaScript ?
In this article we are going to learn how to change JSON String into an object in javascript, JSON stands for JavaScript object notation. It is the plain format used frequently as a communication medium on the internet. It appears close to OOP language like JavaScript but cannot be accessed like JavaScript. It consists of properties enclosed within
3 min read