I want to update an array of objects with a type 'person' to have unique identifiers like 'person0', 'person1', and so on. Currently, the setup looks like this:
var population = [];
var populationCount = 0;
function person(id, age) {
//simplified version
this.id = id;
this.age = age;
}
function createPerson() {
population[populationCount] = new person();
population[populationCount].id = populationCount;
population[populationCount].age = 0;
populationCount++;
}
for (var i = 0; i < 10; i++) {
createPerson();
}
At the moment, the array holds instances as "person, person, person, ..." but my goal is to have it as "person0, person1, person2, ...".
I see the value in doing this because if, for example, population[100] were to die, their place would be taken by population[101], assuming I just use population.splice[100] when they pass away. With that change, population[100] would then have the ID 101, and having distinct 'names' in the array could make locating individuals easier using indexOf.