Use array.push() to add an item to the end of the array.
var sample = new Array();
sample.push(new Object());
To do this n times use a for loop.
var n = 100;
var sample = new Array();
for (var i = 0; i < n; i++)
sample.push(new Object());
Note that you can also substitute new Array() with [] and new Object() with {} so it becomes:
var n = 100;
var sample = [];
for (var i = 0; i < n; i++)
sample.push({});
Answer from Daniel Imms on Stack OverflowSo I made an array with objects. For example here, let's use people
var people = [
{
id: 0,
name: "John Doe",
age: 47
},
{
id: 1,
name: "Jane Doe",
age: 88
},
{
id: 2,
name: "Mason Louis",
age: 17
}
];
so now I can use people[1].name etc for accessing the data on each of these people. Someone just saw my code and said "That looks like total S**T".
What is the proper way to do this? I was thinking this way was fine, especially since it resembles JSON
EDIT: Would it be better to create a constructor and fill an array with instances?
javascript - Declaring array of objects - Stack Overflow
What is the best way to create an array of objects in Javascript? - Stack Overflow
java - Object of arrays or array of objects? - Game Development Stack Exchange
Array of objects, best way to do it.
Use array.push() to add an item to the end of the array.
var sample = new Array();
sample.push(new Object());
To do this n times use a for loop.
var n = 100;
var sample = new Array();
for (var i = 0; i < n; i++)
sample.push(new Object());
Note that you can also substitute new Array() with [] and new Object() with {} so it becomes:
var n = 100;
var sample = [];
for (var i = 0; i < n; i++)
sample.push({});
Depending on what you mean by declaring, you can try using object literals in an array literal:
var sample = [{}, {}, {} /*, ... */];
EDIT: If your goal is an array whose undefined items are empty object literals by default, you can write a small utility function:
function getDefaultObjectAt(array, index)
{
return array[index] = array[index] || {};
}
Then use it like this:
var sample = [];
var obj = getDefaultObjectAt(sample, 0); // {} returned and stored at index 0.
Or even:
getDefaultObjectAt(sample, 1).prop = "val"; // { prop: "val" } stored at index 1.
Of course, direct assignment to the return value of getDefaultObjectAt() will not work, so you cannot write:
getDefaultObjectAt(sample, 2) = { prop: "val" };
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
The array should be like this...
var cup = [];
After we putting properties to the array, it will be like this
[
{
"color": "blue",
"size": "large",
"type": "mug"
}
]
And you can put properties like this..
var cup = [];
cup.push({
color : 'blue',
size : 'large',
type : 'mug'
})
console.log(cup);
The common terminology is "structure of arrays" (SOA) and "array of structures" (AOS) which come from C and is most often seen in terms of SIMD work.
Typically, the AOS approach is faster, if used appropriately, but SOA tends to be easier to work with (and hence optimizes for the more important quality - development time).
SOA, especially in Java, means that your data can remain tightly packed in memory. You can iterate over properties and expect the CPU cache and such to remain happy. With AOS, especially in Java, every object ends up allocated "somewhere" in memory. Iterating over objects could potentially thrash your CPU cache pretty heavily.
In the end, I would take whichever approach you find easiest to use. Your development time is far more valuable than whether your game supports 10 year old PCs or only 9 year old PCs (you're very unlikely to be doing anything htat needs the latest hardware).
There's no reason you can't have both, using the Facade pattern to translate from one interface to the other underlying representation. For example, using Sean's SOA/AOS terms:
SOA facade
class PeopleFacade {
Person persons[5000];
getThirst(int i) { return persons[i].thirst; }
}
AOS facade
class People { int thirsts[5000]; } people;
class PersonFacade {
int i;
getThirst() { return people.thirsts[i]; }
}
This way you can freely choose between a form you are comfortable with using, as a developer interface, vs whatever's best as an implementation for whatever reason, including efficiency/cache reasons.
Another advantage to the facade is that it leads very naturally to the Flyweight pattern, where you use an interface to represent much more persons than are actually in memory. For example, perhaps you have robotic patrons which are never thirsty; then you can put that special case into your PersonFacade, and users of that interface never have to know about robots:
class People { int nonRobotThirsts[1000]; } people;
class PersonFacade {
int i;
bool isRobot;
getThirst() {
if (isRobot)
return 0;
else
return people.nonRobotThirsts[i];
}
}
... or using a more OO approach, you'd have a separate Robot class which acts exactly like a Person except for getThirst().