JavaScript

Creating List of Objects in JavaScript

While maintaining a record in JavaScript, we often come across situations where we need to include the entries of multiple data types. For instance, when we want to relate some feature with respect to a specific attribute. In such a scenario, creating a list of objects in JavaScript becomes very efficient and flexible in the declaration.

This blog will illustrate the methods to create objects list in JavaScript.

How to Create a List of Objects in JavaScript?

To create a list of objects in JavaScript, the following approaches can be utilized:

The mentioned approaches will be discussed one by one!

Method 1: Create a List of Objects in JavaScript Using for Loop

The “for” loop is applied to iterate along all the items by specifying the start and end numbers or index. This technique can be utilized to iterate along the list of objects by specifying the array length and displaying the values simultaneously.

Look at the following example.

Example

In the following example, we will create a list of objects named “objectList” having the following values:

let objectList = [

  { Name: 'Harry', id: 1 , city: "NewYork"},

  { Name: 'David', id: 2 , city: "Berlin"},

  { Name: 'John', id: 3 , city: "London"}

];

Now, apply the “for” loop and the “length” property to iterate along the list objects and print the objects list on the console:

for (let i = 0; i < objectList.length; i ++ ){

console.log(objectList[i]);

}

The corresponding output will be:

Method 2: Create a List of Objects in JavaScript Using forEach() Method

The “forEach()” method calls a function for each array element. This method can be implemented to assign the created objects to each array item and append it to a newly created list.

The following example explains the stated concept.

Example

First, create an array named “objectList” with the following items:

var objectList = ["Linux Hint", "Google"];

Next, apply the “forEach()” method to the created array to call the specified function for each array element. Here, “entry” in the function’s argument refers to the array values. After that, an empty list named “newObj” will be declared to be appended to the list of objects. Now, two object properties named “type” and “value” will be created in each iteration; the type is assigned as “Website”, and the value refers to “entry”(array values). Therefore, a new list(newObj) will be appended with the objects and displayed:

objectList.forEach(function(entry) {

var newObj = {};

newObj['type'] = 'Website';

newObj['value'] = entry;

  console.log(newObj)

});

Output

Method 3: Create a List of Objects in JavaScript Using map() Method

The “map()” method calls a function once for each array element. This method can be implemented to map the specific objects to array elements and append them to a newly created list.

Look at the following example.

Example

Firstly, create an array named “objectList” as discussed in the previous method:

var objectList = ["JavaScript", "Java", "Python"];

Next, apply the “map()” method in order to map the function on the array. Also, create a null list named “newObj” and create two object properties in each iteration, as discussed in the previous method. Finally, log the list of objects on the console:

objectList.map(function(entry) {

var newObj = {};

newObj['type'] = 'language';

newObj['value'] = entry;

  console.log(newObj)

});

Output

We have discussed all the creative methods to create a list of objects in JavaScript.

Conclusion

To create a list of objects in JavaScript, utilize the “for” loop method to iterate along the list objects with the help of the length property, the “forEach()” method to relate the newly created objects with the array values and append it to a new list, or the “map()” method to map the function on the created array in order to access the array items, merge them with the created objects and append them in the objects list. This blog demonstrated the methods to create an object’s list in JavaScript.

About the author

Umar Hassan

I am a Front-End Web Developer. Being a technical author, I try to learn new things and adapt with them every day. I am passionate to write about evolving software tools and technologies and make it understandable for the end-user.