2016-12-28 151 views
0

我有這些都是類型「人」對象的數組,但我想在數組中含有可能像「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

回答

4

您混淆了的標識符的類型。

您製作的每個實例都是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,所以我沒有辦法工作,我需要重置所有這些數組...... –

0

,說你想要擁有person0,person1,person2,...你說你想要無限數量的不同類型,而不僅僅是不同的對象。我不知道動態創建類型的方法。正如在評論中所說的,你將不得不鍵值對。爲什麼不給每個人一個獨特的標識符,我相信你已經在做。

你可以有嵌套的對象內的那些對象,比方說:

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