Monday, June 3, 2024
 Popular · Latest · Hot · Upcoming
139
rated 0 times [  143] [ 4]  / answers: 1 / hits: 33074  / 8 Years ago, tue, december 20, 2016, 12:00:00

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';

More From » arrays

 Answers
7

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


[#59631] Monday, December 19, 2016, 8 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
rossj

Total Points: 606
Total Questions: 100
Total Answers: 116

Location: Dominican Republic
Member since Sun, Sep 4, 2022
2 Years ago
;