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:
Now, apply the “for” loop and the “length” property to iterate along the list objects and print the objects list on the console:
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:
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:
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:
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:
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.