How to Initialize an Array of Objects in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 4, 2024
3 min

banner

# Initialize an Array of Objects in JavaScript

To initialize an array of objects:

  1. Use the Array() constructor to create an array of N empty elements.
  2. Pass the array and a map() function to the Array.from() method.
  3. Return an object on each iteration of the map() function.
index.js
const arr = Array.from(Array(2), () => ({ key: 'value', })); // ๐Ÿ‘‡๏ธ [ { key: 'value' }, { key: 'value' } ] console.log(arr);

initialize array of objects in javascript

The code for this article is available on GitHub

You also have access to the index in the map() function.

index.js
const arr = Array.from(Array(2), (_, index) => ({ id: index, key: 'value', })); // ๐Ÿ‘‡๏ธ [ { id: 0, key: 'value' }, { id: 1, key: 'value' } ] console.log(arr);

you also have access to the index

The Array.from() method creates a new array from the provided iterable.

The second argument we passed to the method is a map() function.

The function gets invoked with each element in the array.

On each iteration, we return an object.

The Array.from() method returns a new array containing the elements that the map function returned.

# Initialize an Array of Objects using fill()

This is a three-step process:

  1. Use the Array() constructor to create an array of N empty elements.
  2. Use the Array.fill() method to fill the array with undefined values.
  3. Use the map() method to fill the array with objects.
index.js
const arr = new Array(2).fill().map(() => ({ key: 'value', })); console.log(arr); // ๐Ÿ‘‰๏ธ [ { key: 'value' }, { key: 'value' } ]

initialize an array of objects using fill

The code for this article is available on GitHub

You can also access the index when using the Array.map() method.

index.js
const arr1 = new Array(2).fill().map((_, index) => ({ id: index, })); console.log(arr1); // ๐Ÿ‘‰๏ธ [ { id: 0 }, { id: 1 } ]

The argument we passed to the Array() constructor is the number of empty elements the array should contain.

index.js
console.log(new Array(2)); // ๐Ÿ‘‰๏ธ [ , ]

The Array() constructor sets the array's length to the provided value.

index.js
const arr = []; arr.length = 2; console.log(arr); // ๐Ÿ‘‰๏ธ [ , ]

We used the Array.fill() method to fill the array with undefined values.

index.js
// ๐Ÿ‘‡๏ธ [ undefined, undefined ] console.log(new Array(2).fill());

This is necessary because the map() function doesn't iterate over empty elements.

We could have passed an object directly to the Array.fill() method to fill the array with objects.

However, note that the objects in the array would store the same reference.

index.js
const arr = new Array(2).fill({key: 'value'}); // ๐Ÿ‘‡๏ธ [{key: 'value'}, {key: 'value'}] console.log(arr); arr[0].anotherKey = 'another value'; // [ // { key: 'value', anotherKey: 'another value' }, // { key: 'value', anotherKey: 'another value' } // ] console.log(arr);

This is why we used the Array.map() method to iterate over the array and fill it with objects stored in different locations in memory.

Alternatively, you can use a simple for loop.

# Initialize an Array of Objects using for loop

This is a three-step process:

  1. Use the Array() constructor to create an array of N empty elements.
  2. Use a for loop to iterate over the array.
  3. Assign an object to each element.
index.js
const arr = new Array(2); for (let i = 0; i < arr.length; i++) { arr[i] = {key: 'value'}; } // ๐Ÿ‘‡๏ธ [{key: 'value'}, {key: 'value'}] console.log(arr);

initialize array of objects using for loop

The code for this article is available on GitHub

We used the Array() constructor to create an array of 2 empty elements.

The for loop allows us to iterate over the array and assign an object to each empty element.

We can't use the Array.forEach() method to iterate over the array because it skips empty elements.

Instead, we used the array's length in a basic for loop.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev