I understand that in Javascript, I can create an object like this:
var cup = {};
Furthermore, I can set properties like this:
cup.color = 'Blue';
cup.size = 'Large';
cup.type = 'Mug';
Can I create an array of cups? For example:
cup[0].color = 'Blue';
cup[1].size = 'Large';
cup[2].type = 'Mug';
Creating an array of objects We can represent it as an array this way: let cars = [ { "color": "purple", "type": "minivan", "registration": new Date('2017-01-03'), "capacity": 7 }, { "color": "red", "type": "station wagon", "registration": new Date('2018-03-03'), "capacity": 5 }, { ... }, ... ]
An Array of Objects is created using the Object class, and we know Object class is the root class of all Classes. We use the Class_Name followed by a square bracket [] then object reference name to create an Array of Objects.
Iterating through an array is possible using For loop, For..in, For..of, and ForEach(). Iterating through an array of objects is possible using For..in, For..of, and ForEach().
An array can be created using array literal or Array constructor syntax. Array literal syntax: var stringArray = ["one", "two", "three"]; Array constructor syntax: var numericArray = new Array(3); A single array can store values of different data types.
Creating an array is as simple as this:
var cups = [];
You can create a populated array like this:
var cups = [
{
color:'Blue'
},
{
color:'Green'
}
];
You can add more items to the array like this:
cups.push({
color:"Red"
});
MDN array documentation
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With