不同的名称相同类型的不同对象

问题描述:

我有这些都是类型“人”对象的数组,但我想在数组中含有可能像“person0”,“PERSON1”,等我现在有这:不同的名称相同类型的不同对象

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(); 
} 

数组目前包含 “人,人,人,......”,但我想它含有 “person0,PERSON1,PERSON2,...”。

为什么我认为这将是有用的...如果假设人口[100]会死,那将是ID为100的人,人口[101]将取代它,假设我只是简单地使用当他死亡时,人口爆炸[100]。所以,现在的人口[100]的ID为101,现在这将是有用的,如果人口的阵列将包含不同的“名字”,所以你可以使用的indexOf得到任何具体的人的指数...

+1

为什么不在这种情况下使用一个对象(键值)? – UnholySheep

+0

@UnholySheep因为我不想要对象本身的值,而是阵列中的对象只是不同的“名称”而不是人,人,人 –

+0

这没有什么意义。什么是对象的“名称”应该是?它在代码方面没有意义,因为您可以使用任何名称的变量来引用对象。此外,这是什么用途呢?您只能通过索引访问数组元素,那么如何使用这个“名称”? – UnholySheep

您混淆了的标识符的类型。

您制作的每个实例都是person的一个实例,这不会改变。对象类型是对象类型 - person[6]仍然是person的实例。

id属性或索引将是区分person与下一个实例的最佳方法。

而且,你的结构是有点过这里。 person是一个构造函数,它接受两个参数,但在构造一个人时不传递这些参数。相反,您将它们设置为属性,这是可以的,但是与您的代码编写方式相反。而且,你的函数应该将创建人与将其添加到数组中去耦。

这种结构将是一个更好的办法:

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

如果您担心不匹配id小号指数,你总是可以创建一个“重置”功能,像这样:

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

而且,如果你希望能够找到基础上,id阵列中的人不知道对应的指数是什么,你可以这样做:

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

+0

更容易只会说: –

+0

for(var i in population){ –

+0

sry about that ...更容易就是说:for(var i in population){population [i] .id = i; }'我用过这个,但是因为我在不同的数组中使用了id,所以我没有办法工作,我需要重置所有这些数组...... –

,说你想要拥有person0,person1,person2,...你说你想要无限数量的不同类型,而不仅仅是不同的对象。我不知道动态创建类型的方法。正如在评论中所说的,你将不得不键值对。为什么不给每个人一个独特的标识符,我相信你已经在做。

你可以有嵌套的对象内的那些对象,比方说:

var persons = [ 
    { 
     person0: 
     { 
      id: 0, 
      age: 69, 
      name: 'john doe' 
     }, 
     person1: 
     { 
     ... 
     }, 
     ... 
    } 
]