Different Ways to Crate an Array of Objects in JavaScript ?
Last Updated :
08 Feb, 2024
Objects in JavaScript are key-value pairs, making them suitable for representing structured data. Also, an array in JavaScript is a versatile data structure that can hold a collection of values. When dealing with objects, an array can be used to store multiple objects. An array of objects allows you to organize and manage multiple sets of related information. The key-value pairs in each object within the array represent the attributes or properties of the items being stored.
Below are the approaches used to create an array of objects in javascript:
Approach 1: Using Literals
This is most easiest way of creating array of objects. You directly define an array with objects using square brackets and curly braces.
Example: Here we will create an array of objects in JavaScript, where we create a fruit array having price and name of fruit as an object inside that.
JavaScript
let fruits = [
{ name: 'Apple', price: 40 },
{ name: 'Banana', price: 20 },
{ name: 'Orange', price: 100 }
];
// printing the values of array
console.log(fruits)
Output[
{ name: 'Apple', price: 40 },
{ name: 'Banana', price: 20 },
{ name: 'Orange', price: 100 }
]
Approach 2: Using Array Constructor
You can use the Array constructor to create an array of objects. However, the literal syntax is generally preferred. Array constructor can be created using 'new' keyword.
Example: Here, We will take an example to create array of objects using array constructor. we will create an array of cars and inside that we will create an objects having name and color.
JavaScript
let cars = new Array(
{ name: 'Maruti', color: 'black' },
{ name: 'Audi', color: 'blue' },
{ name: 'Lamborghini', color: 'yellow' }
);
console.log(cars);
Output[
{ name: 'Maruti', color: 'black' },
{ name: 'Audi', color: 'blue' },
{ name: 'Lamborghini', color: 'yellow' }
]
Approach 3: Using Push method
You can start with an empty array and use the push method to add objects dynamically. Array Push method simply add objects inside the define array.
Example: Here, we will take help of push method to create array of objects and create a array name students and inside that there is student name and age as an object.
JavaScript
let student=[];
student.push({ name: 'Mahi', age: 24 });
student.push({ name: 'Nick', age: 23 });
student.push({ name: 'Joy', age: 22 });
console.log(student)
Output[
{ name: 'Mahi', age: 24 },
{ name: 'Nick', age: 23 },
{ name: 'Joy', age: 22 }
]
Approach 4: Using Object Spread Syntax with concat
This method utilizes the concat
method to concatenate multiple arrays, with each object being spread into individual elements of the array. It creates a new array containing all the objects specified.
Example: Here, we have used concat method to create array of objects.
JavaScript
const arrayOfObjects = [].concat(
{ key1: 'value1', key2: 'value2' },
{ key1: 'value3', key2: 'value4' }
);
console.log(arrayOfObjects);
Output[
{ key1: 'value1', key2: 'value2' },
{ key1: 'value3', key2: 'value4' }
]
Similar Reads
Difference between Array and Array of Objects in JavaScript ArrayAn Array is a collection of data and a data structure that is stored in a sequence of memory locations. One can access the elements of an array by calling the index number such as 0, 1, 2, 3, ..., etc. The array can store data types like Integer, Float, String, and Boolean all the primitive dat
3 min read
JavaScript - Create an Object From Two Arrays Here are the different methods to create an object from two arrays in JavaScript1. Using for-each loopThe arr.forEach() method calls the provided function once for each element of the array. JavaScriptconst a1 = ['name', 'age', 'city']; const a2 = ['Ajay', 25, 'New Delhi']; const res = {}; a1.forEac
3 min read
Convert Dictionary into an Array of objects in JavaScript In JavaScript, dictionaries also known as objects are used to store key-value pairs. But sometimes you might need to convert this data into an array of objects so that you can handle or utilize particular parts more easily. Below are the multiple ways of converting a Dictionary into an Array of obje
4 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
Difference Between JavaScript Arrays and Objects Below are the main differences between a JavaScript Array and Object.FeatureJavaScript ArraysJavaScript ObjectsIndex TypeNumeric indexes (0, 1, 2, ...)Named keys (strings or symbols)OrderOrdered collectionUnordered collectionUse CaseStoring lists, sequences, ordered dataStoring data with key-value p
1 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
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
Create an Array of Given Size in JavaScript The basic method to create an array is by using the Array constructor. We can initialize an array of certain length just by passing a single integer argument to the JavaScript array constructor. This will create an array of the given size with undefined values.Syntaxconst arr = new Array( length );J
3 min read
JavaScript - Convert Two-Dimensional Array Into an Object Here are the different methods to convert the two-dimensional array into an object in JavaScript.1. Using a for LoopThe simplest way to convert a two-dimensional array into an object is by iterating through the array using a for loop and assigning key-value pairs.JavaScriptconst a = [['name', 'Abhay
2 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