Open In App

How to Push an Object into an Array using For Loop in JavaScript ?

Last Updated : 01 Feb, 2024
Improve
Improve
Like Article
Like
Save
Share
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.

Using simple for-loop

This 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);


Output

Array before pushing: []
Array after pushing: [
  {
    title: 'Introduction to JavaScript',
    author: 'Geek123',
    views: 1000
  }
]

Using for…of Loop

This 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);


Output

Array before pushing: []
Array after pushing: [
  {
    title: 'Introduction to JavaScript',
    author: 'Geek123',
    views: 1000
  }
]

Using forEach Loop

This 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);


Output

Array before pushing: []
Array after pushing: [
  {
    title: 'Introduction to JavaScript',
    author: 'Geek123',
    views: 1000
  }
]


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads