Reputation: 31
I have an array of objects which are all the type 'person' but I would like the array to contain maybe something like 'person0', 'person1', etc. I currently have 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();
}
The array currently contains "person, person, person, ..." but I would like it to contain "person0, person1, person2, ...".
Why I think this would be useful ... if let's say population[100] would die, that would be the person with the id of 100, population[101] would take its place, assuming I just simply use population.splice[100] when he dies. So now population[100] has the id 101 and now it would be useful if the array of population would contain different 'names' so you could use indexOf to get the index of any specific person ...
Upvotes: 1
Views: 360
Reputation: 65806
You are confusing a type with an identifier.
Each instance that you make is an instance of a person
, that's not going to change. Object types are object types - - person[6]
is still an instance of a person
.
The id
property or an index would be the best approaches to differentiate one instance of person
from the next.
Also, your structure is a bit off here. person
is a constructor function that takes two arguments, but you don't pass those arguments when you construct a person. Instead you are setting them as properties, which is OK, but counter-intuitive to the way your code is written. And, your createPerson
function should decouple the creation of the person from the addition of it to the array.
This structure would be a better approach:
var population = [];
var populationCount = 0;
// By convention, constructor funcitons should be Pascal-Case
function Person(id,age){
//simplified version
this.id = id;
this.age = age;
}
function createPerson(){
// To make this function more true to its name, just have it return
// a new Person. It shouldn't be this funciton's job to know where
// or how that Person is going to be used.
// Also, just pass the arguments that the function is expecting
return new Person(++populationCount, 0);
}
for(var i = 0; i < 10; i++){
// Here, you have a plan for how to use the Person, so here is where
// you should add it to the array. By decoupling the addition of the
// Person to the array from the creation of the Person, the "createPerson"
// function becomes more useful.
population[populationCount] = createPerson();
}
console.log("The population consists of: ", population);
console.log("The last person in the population is: ", population[population.length - 1]);
console.log("The third person in the population is a: ", population[2].constructor.name);
console.log("The third person in the population has an id of: " + population[2].id);
If you are concerned about indexes not matching id
s, you could always create a "reset" function, like this:
var population = [];
var populationCount = 0;
function Person(id,age){
this.id = id;
this.age = age;
}
function createPerson(){
return new Person(++populationCount, 0);
}
for(var i = 0; i < 10; i++){
population[populationCount] = createPerson();
}
// Now, kill off the first 5 people:
population.splice(0,5);
console.log("First 5 people are gone. Array now contains: " + population.length + " people.");
console.log("First person's id is: " + population[0].id);
// *****************************************************************************
// Create a new array with the old members, but update the ids
var newPopulation = [];
function rePopulate(ary){
newPopulation = ary.map(function(element){
element.id = newPopulation.length + 1;
return element;
});
}
rePopulate(population);
console.log("The array has been recreated and ids have been adjusted");
console.log("First person's id is now: " + newPopulation[0].id);
And, if you want to be able to find a Person in the array based on the id
without knowing what the corresponding index is, you can do this:
var population = [];
var populationCount = 0;
function Person(id,age){
this.id = id;
this.age = age;
}
function createPerson(){
return new Person(++populationCount, 0);
}
for(var i = 0; i < 10; i++){
population[populationCount] = createPerson();
}
// Now, kill off the first 5 people:
population.splice(0,5);
console.log("First 5 people are gone. Array now contains: " + population.length + " people (id's 6 - 10).");
console.log("First person's id is: " + population[0].id);
// *****************************************************************************
// With a simple "find" function, you can locate the correct person based on
// their id and you don't have to worry about the index at all.
function findPerson(id) {
return population.find(function(p){
return p.id === id;
});
}
var x = 7; // Whatever id you are looking for
console.log("Person with id of 7 is: ", findPerson(x));
Upvotes: 4
Reputation: 2862
By saying you would like to have person0, person1, person2, ... You are saying you want infinite number of different types, not just different objects. I do not know of a way to dynamically create types. As said in the comments, you would have to key-value pairs. Why not give each person a unique identifier, which I believe you are already doing.
You could have those objects nested within an object, say:
var persons = [
{
person0:
{
id: 0,
age: 69,
name: 'john doe'
},
person1:
{
...
},
...
}
]
Upvotes: 0