目前我有一个填充数组的问题,我在代码中调用了input
,我的代码中调用了athlete
。使用其他几个数组实例化了名为athlete
的对象。我附加了一个jsfiddle链接到这个帖子,这个帖子基本上是一个简化版本,具有相同的问题。我知道逻辑可能看起来多余,但是这是因为这个例子是必要的(实际的逻辑将与用户输入一起工作)。用Javascript中的对象填充数组
我的问题是我正在用new Athlete
对象填充数组,但我无法访问数组中对象的特定属性。
我是新来处理对象,所以我会很感激任何关于如何使这项工作的建议。我添加了最后一行代码来显示我的输入数组。
var input = new Array();
var girl=[1,1,1,1];
var boy = [1,1,1,1];
var balleyscore = [100,400,320,50];
var boyHeight = [72,68,65,75];
var girlHeight=[60,57,65,55];
var boyAge = [19,32,22,25];
var girlAge = [20,15,32,18];
//the above are all generate from user inputs
// they go into the object constructor below
function athlete(girl, boy,balleyscore, height, age){
this.girl=girl;
this.boy=boy;
this.balleyscore=balleyscore;
this.height=height;
this.age = age;
};
function insertToObjectArray()
{
var j=0;
var i=0; //insert all our data into one array of the load object
for(j = 0; j < girl.length;j++)
{
input[j]= new athlete(true,false,balleyscore[j],girlHeight[j],girlAge[j]);
}
for(j = girl.length; j <=girl.length+boy.length;j++)
{
input[j]= new athlete(false,true,0,boyHeight[i],boyAge[i]);
i++;
}
};
insertToObjectArray();
document.getElementById("demo").innerHTML=input;
'我无法访问在array.'对象的特定属性是不够的调试程序。 :( – thefourtheye
你试图访问哪个属性?究竟什么不能按照你的预期工作? –
我需要能够访问每个属性 – bala