Reputation: 503
I use a constructor to create an object
function player(name,state,score,marker,combination) {
this.name = name;
this.state = state;
this.score = score;
this.marker = marker;
this.combination = combination;
}
Where property "combination" should be an array. At the point of creating of an PLAYER object instance, I set the array this way:
players[players.length] = new player(name,false,0,'marker',[]);
Then I dynamically set the values to the array inside the "combination" property, but I don't know how to find the length of the current array
players[i].combination[ANONYMOUS ARRAY.length] = some value;
I see that I'm missing some crucial knowledge here. Could please someone help me out here? Suggest the proper manner of either setting an array inside the object's property / treating such an array???
Upvotes: 0
Views: 176
Reputation: 1075567
It would be players[i].combination.length
, because players[i].combination
is the reference to the array:
players[i].combination[players[i].combination.length] = some_value;
Or of course:
players[i].combination.push(some_value);
Upvotes: 3
Reputation: 135
I'd use push() instead of putting an index and guessing the length, so instead of:
players[players.length] = new player(name,false,0,'marker',[]);
do
players.push(new player(name,false,0,'marker',[]);
Instead of
players[i].combination[ANONYMOUS ARRAY.length] = some value;
I guess you know the value of 'i', do players[i].combination.push(some_value)
or if you don't know i then just create the combination array and pass it when creating players
Upvotes: 2