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

时间:2016-12-28 18:57:34

标签: javascript arrays object

我有一系列对象,这些对象都属于“人”类型。但我希望这个数组可能包含类似于' 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();
}

阵列目前包含&#34;人,人,人,...&#34;但我希望它包含&#34; person0,person1,person2,...&#34;。

为什么我认为这会有用...... 如果我们说人口[100]会死,那就是身份100的人,人口[101]会取而代之,假设我只是简单地使用population.splice [100]。所以现在人口[100]的id为101,现在如果人口数组中包含不同的名称,那将是有用的。所以你可以使用indexOf获取任何特定人的索引......

2 个答案:

答案 0 :(得分:4)

您会混淆带有标识符的类型。

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

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

此外,你的结构有点偏。 person是一个带有两个参数的构造函数,但是当你构造一个人时,你不会传递这些参数。相反,您将它们设置为属性,这是正常的,但与您的代码编写方式相反。并且,您的createPerson函数应该将该人的创建与其添加到数组中分离。

这种结构是一种更好的方法:

&#13;
&#13;
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);
&#13;
&#13;
&#13;

如果您担心索引与id不匹配,您可以随时创建&#34;重置&#34;功能,像这样:

&#13;
&#13;
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);
&#13;
&#13;
&#13;

并且,如果您希望能够在不知道相应索引的情况下基于id在数组中找到Person,则可以执行以下操作:

&#13;
&#13;
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));
&#13;
&#13;
&#13;

答案 1 :(得分:0)

通过说你想拥有person0,person1,person2,......你说你想要无数种不同的类型,而不仅仅是不同的对象。我不知道动态创建类型的方法。如评论中所述,您必须使用键值对。为什么不给每个人一个唯一的标识符,我相信你已经在做了。

您可以将这些对象嵌套在对象中,例如:

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