Sometimes, we want to create list of objects in JavaScript.
In this article, we’ll look at how to create list of objects in JavaScript.
How to create list of objects in JavaScript?
To create list of objects in JavaScript, we can put objects in an array.
For instance, we write
const list = [
{ date: "12/1/2022", reading: 3, id: 20055 },
{ date: "13/1/2022", reading: 5, id: 20053 },
{ date: "14/1/2022", reading: 6, id: 45652 },
];
to put 3 objects in the square brackets to populate the list
array.
Then we access an object’s value with
console.log(list[1].date);
We use list[1].date
to get the 2nd object’s date
property value.
Conclusion
To create list of objects in JavaScript, we can put objects in an array.