How to Push an Object into an Array using For Loop in JavaScript ? Last Updated : 01 Feb, 2024 Comments Improve Suggest changes Like Article Like Report 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 for a better understanding of the problem statement. Table of Content Using simple for-loopUsing for...of LoopUsing forEach LoopUsing simple for-loopThis method uses a simple for-loop to push the array object into the array. We are printing the array before pushing of object and also printing the array after pushing the object into the array. Syntax:for (initialization; condition; update) { // code}Example: The below code uses a simple for-loop to push an object into an array using a for-loop in JavaScript. JavaScript let arr = []; let arrObj = { title: "Introduction to JavaScript", author: "Geek123", views: 1000, }; console.log("Array before pushing:", arr); for (let i = 0; i < 1; i++) { arr.push(arrObj); } console.log("Array after pushing:", arr); OutputArray before pushing: [] Array after pushing: [ { title: 'Introduction to JavaScript', author: 'Geek123', views: 1000 } ] Using for...of LoopThis method uses for...of loop to push the objects into an array where the array is initially empty. The for of loop iterates over the array to push the objects into the array. Syntax:for (variable of iterable) { // code}Example: The below code uses for...of Loop to push an object into an array using a for-loop in JavaScript. JavaScript let arr = []; let arrObj = { title: "Introduction to JavaScript", author: "Geek123", views: 1000, }; console.log("Array before pushing:", arr); for (let item of Array(1).fill(arrObj)) { arr.push(item); } console.log("Array after pushing:", arr); OutputArray before pushing: [] Array after pushing: [ { title: 'Introduction to JavaScript', author: 'Geek123', views: 1000 } ] Using forEach LoopThis method uses the forEach loop, there is an empty array arr and the arrObj. When the forEach loop is executed, the loop pushes the object into the array and the array is been updated with the object which is been pushed. We are printing the array as output. Syntax:array.forEach(function(currentValue, index, array) { // code});Example: The below code uses a forEach Loop to push an object into an array using a for-loop in JavaScript. JavaScript let arr = []; let arrObj = { title: "Introduction to JavaScript", author: "Geek123", views: 1000, }; console.log("Array before pushing:", arr); Array(1) .fill(arrObj) .forEach((item) => arr.push(item)); console.log("Array after pushing:", arr); OutputArray before pushing: [] Array after pushing: [ { title: 'Introduction to JavaScript', author: 'Geek123', views: 1000 } ] Comment More infoAdvertise with us Next Article How to Push an Object into an Array using For Loop in JavaScript ? G gauravgandal Follow Improve Article Tags : JavaScript Web Technologies Geeks Premier League JavaScript-Questions Geeks Premier League 2023 +1 More Similar Reads How to Push an Array into Object in JavaScript? 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() MethodThe array push() method adds one 2 min read How to Create an Array of Object Literals in a Loop using JavaScript? Creating arrays of object literals is a very frequent activity in JavaScript, as often when working with data structures, several objects are needed. This article will allow your hand through different ways of coming up with an array of object literals in a loop in JavaScript. Here are different app 5 min read How to Convert Array into Array of Objects using map() & reduce() in JavaScript? An array of objects can contain multiple objects with the same properties i.e. key-value pairs. But a map does not contain duplicate values which means if you convert an array of objects with duplicate objects into a map, it will contain only the unique key-value pairs. Below are the approaches to c 2 min read How to Move a Key in an Array of Objects using JavaScript? The JavaScript array of objects is a type of array that contains JavaScript objects as its elements.You can move or add a key to these types of arrays using the below methods in JavaScript:Table of ContentUsing Object Destructuring and Map()Using forEach() methodUsing for...of LoopUsing reduce() met 5 min read How to Store an Object Inside an Array in JavaScript ? Storing an object inside an array in JavScript involves placing the object as an element within the array. The array becomes a collection of objects, allowing for convenient organization and manipulation of multiple data structures in a single container. Following are the approaches through which it 3 min read How to Convert an Object into Array of Objects in JavaScript? Here are the different methods to convert an object into an array of objects in JavaScript1. Using Object.values() methodObject.values() method extracts the property values of an object and returns them as an array, converting the original object into an array of objects.JavaScriptconst a = { java: 3 min read How to use forEach with an Array of Objects in JavaScript ? Using the forEach() method with an array of objects in JavaScript is essential for iterating over collections and performing operations on each object. This guide explores effective techniques to utilize forEach() for array manipulation, enhancing your coding skills. Syntax: array.forEach( function( 3 min read How to Loop Through an Array in JavaScript? Here are the various ways to loop through an array in JavaScript1. Using for LoopThe for loop is one of the most used ways to iterate over an array. It gives you complete control over the loop, including access to the array index, which can be useful when you need to modify elements or perform other 4 min read How to Add an Image File to an Object of an Array in JavaScript ? In JavaScript adding the image files into objects within an array is essential for managing and organizing data in web development. This practice is particularly valuable when dealing with collections of objects, each requiring an associated image. There are various ways to add an image file to an o 3 min read How to convert a map to array of objects in JavaScript? A map in JavaScript is a set of unique key and value pairs that can hold multiple values with only a single occurrence. Sometimes, you may be required to convert a map into an array of objects that contains the key-value pairs of the map as the values of the object keys. Let us discuss some methods 6 min read Like