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 Iterate an Array using forEach Loop in JavaScript? Iterating through an array in JavaScript can be done in multiple ways. One of the most common methods is using the traditional for loop. The modern approach to iterate the array which is by using the forEach method. This method is different from the traditional approach as it uses a functional appro 1 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 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 5 min read How to convert Object's array to an array using JavaScript ? Given an array of objects and the task is to convert the object values to an array with the help of JavaScript. There are two approaches that are discussed below: Approach 1:We can use the map() method and return the values of each object which makes the array. Example: html<!DOCTYPE HTML> 2 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 Like