JavaScript的新手(通常在Excel函数之外进行编码),希望有人可以帮助我指出正确的方向。我已经在网上搜索过,但没有找到明确的答案。
我有两个数组-一个带有星期几,另一个带有人们的名字和姓氏。我从带有人名的数组中选择一个随机索引号,并尝试将名字和姓氏分配给一天,然后将所有三个值放入一个新数组中。然后,我将这个最终数组打印到HTML表中。
包含日期的数组是一维的,只是从星期日到星期六。据我所知,包含人们姓名的数组实际上是二维的,即名字和姓氏。
我通过对第三个数组进行硬编码来实现此目的,但是我想动态地创建数组。我似乎无法使array.push()方法正常工作,希望有人愿意提供帮助。
值得注意的是-我正在通过创建另一个数组来选择随机索引号,该数组可以选择介于0和people数组长度之间的七个随机数。在这种情况下,push方法可以正常工作。
我尝试使用array.push()方法,但似乎无法按照我的预期方式工作。它要么返回一个空值,要么看起来像胡言乱语,尽管我认为这是来自人员数组的随机字母。
//This is my working code. The script tag is after the body elements because I need to create some of the body elements before I run the script.
<body>
<table id="Week1Table" class="center">
<tr>
<th class="Week1">Week 1</th>
<th class="Week1">First Name</th>
<th class="Week1">Last Name</th>
</tr>
</table>
<table id="Week2Table" class="center">
<tr>
<th class="Week2">Week 2</th>
<th class="Week2">First Name</th>
<th class="Week2">Last Name</th>
</tr>
</table>
<script>
var people = [
["Andre", "the Giant"],
["Randy", "Savage"],
["Steve", "Austin"],
["The", "Rock"],
["Man", "Kind"],
["Steel", "Cage"],
["I", "Quit"]
];
var week1schedule = [
"Sunday",
"Monday",
"Tuesday",
"Wednesday",
"Thursday",
"Friday",
"Saturday"
];
var week2schedule = week1schedule;
var peoplePicker = []
while(peoplePicker.length < week1schedule.length*2){
var r = Math.floor(Math.random()*people.length);
if(peoplePicker.indexOf(r) === -1) peoplePicker.push(r);
};
//This is the final array, and the one that I would like to create dynamically rather than statically.
var peopleRandomizerTable = [];
peopleRandomizerTable[0] = week1schedule[0];
peopleRandomizerTable[1] = people[peoplePicker[0]][0];
peopleRandomizerTable[2] = people[peoplePicker[0]][1];
peopleRandomizerTable[3] = week1schedule[1];
peopleRandomizerTable[4] = people[peoplePicker[1]][0];
peopleRandomizerTable[5] = people[peoplePicker[1]][1];
peopleRandomizerTable[6] = week1schedule[2];
peopleRandomizerTable[7] = people[peoplePicker[2]][0];
peopleRandomizerTable[8] = people[peoplePicker[2]][1];
peopleRandomizerTable[9] = week1schedule[3];
peopleRandomizerTable[10] = people[peoplePicker[3]][0];
peopleRandomizerTable[11] = people[peoplePicker[3]][1];
peopleRandomizerTable[12] = week1schedule[4];
peopleRandomizerTable[13] = people[peoplePicker[4]][0];
peopleRandomizerTable[14] = people[peoplePicker[4]][1];
peopleRandomizerTable[15] = week1schedule[5];
peopleRandomizerTable[16] = people[peoplePicker[5]][0];
peopleRandomizerTable[17] = people[peoplePicker[5]][1];
peopleRandomizerTable[18] = week1schedule[6];
peopleRandomizerTable[19] = people[peoplePicker[6]][0];
peopleRandomizerTable[20] = people[peoplePicker[6]][1];
peopleRandomizerTable[21] = week2schedule[0];
peopleRandomizerTable[22] = people[peoplePicker[7]][0];
peopleRandomizerTable[23] = people[peoplePicker[7]][1];
peopleRandomizerTable[24] = week2schedule[1];
peopleRandomizerTable[25] = people[peoplePicker[8]][0];
peopleRandomizerTable[26] = people[peoplePicker[8]][1];
peopleRandomizerTable[27] = week2schedule[2];
peopleRandomizerTable[28] = people[peoplePicker[9]][0];
peopleRandomizerTable[29] = people[peoplePicker[9]][1];
peopleRandomizerTable[30] = week2schedule[3];
peopleRandomizerTable[31] = people[peoplePicker[10]][0];
peopleRandomizerTable[32] = people[peoplePicker[10]][1];
peopleRandomizerTable[33] = week2schedule[4];
peopleRandomizerTable[34] = people[peoplePicker[11]][0];
peopleRandomizerTable[35] = people[peoplePicker[11]][1];
peopleRandomizerTable[36] = week2schedule[5];
peopleRandomizerTable[37] = people[peoplePicker[12]][0];
peopleRandomizerTable[38] = people[peoplePicker[12]][1];
peopleRandomizerTable[39] = week2schedule[6];
peopleRandomizerTable[40] = people[peoplePicker[13]][0];
peopleRandomizerTable[41] = people[peoplePicker[13]][1];
var table1 = document.getElementById("Week1Table");
var table2 = document.getElementById("Week2Table");
var peopleRandomizerCount = 0;
//This populates the two tables. Two weeks; one table for each week.
for(var rowCount = 0; rowCount < 7; rowCount++)
{
// create a new row
var newRow = table1.insertRow(table1.length);
for(var columnCount = 0; columnCount < 3; columnCount++)
{
// create a new cell
var cell = newRow.insertCell(columnCount);
// add value to the cell
cell.innerHTML = peopleRandomizerTable[peopleRandomizerCount];
cell.className = "people";
peopleRandomizerCount++;
}
}
for(var rowCount = 0; rowCount < 7; rowCount++)
{
// create a new row
var newRow = table2.insertRow(table1.length);
for(var columnCount = 0; columnCount < 3; columnCount++)
{
// create a new cell
var cell = newRow.insertCell(columnCount);
// add value to the cell
cell.innerHTML = peopleRandomizerTable[peopleRandomizerCount];
cell.className = "people";
peopleRandomizerCount++;
}
}
</script>
</body>
答案 0 :(得分:1)
您的名字和姓氏都不需要使用二维数组,
您可以简单地使用包含名字和姓氏的对象的一维数组,如下所示:
1 eggs 23
2 bread 22
3 juice 20
现在我想您可能知道如何组合2个一维数组。如果没有,请告诉我