var people = new Array();
function People (name, location, age){
this.name = name;
this.location = location;
this.age = age;
}
我还有另外两个函数来生成人员并将它们加载到表中。
function generatePeople(){}
function loadPeopleIntoTable(){}
我基本上需要浏览人员列表,取他们的名字,然后显示该表中出现的最常见的名字。该函数简称为 commonFirstName()。
function commonFirstName(){}
给出的提示是“JavaScript 对象可以按字符串索引”,但我不理解 100%。我已经编写了遍历数组并找到常见名字的代码。但是我不能调用 peoples 数组来遍历该列表 - 我只能让它与 commonFirstName() 中手动创建的数组一起使用。这是为什么?
如果需要,我可以提供进一步的说明。
function commonFirstName(){
alert(people[1]);
//Rest of code that does the occurrences/name here
}
其输出只是 [object Object]。
另一方面:
function commonFirstName(){
tempArray = ['John Smith', 'Jane Smith', 'John Black'];
//Run through algorithm for finding common name.
}
给出“通用名:John。出现 2 次”的警报输出
我曾想过如果我只是通过函数传递数组 people ,例如:
function commonFirstName(people){
alert(people[1]);
}
应该给我一些东西,任何东西。在这一点上,我期待的不仅仅是名字,而是元素 1 或其中一个的全名、位置和年龄。它根本不运行,就好像数组不存在或只是空的一样。
这是我拥有的一切的代码:
var PEOPLECOUNT = 100;
var people = new Array();
function People(name, location, age) {
this.name = name;
this.location = location;
this.age = age;
}
function initPage() {
generateTableRows();
generatePeople();
}
function generateTableRows() {
var table = document.getElementById("ageTable");
var tableBody = table.getElementsByTagName("tbody")[0];
for (var i = 0; i < PEOPLECOUNT; i++) {
var newRow = document.createElement("tr");
newRow.setAttribute("id", "ageRow" + i.toString(10));
var td1 = document.createElement("td");
var td2 = document.createElement("td");
var td3 = document.createElement("td");
td1.setAttribute("class", "dataCell");
td2.setAttribute("class", "dataCell");
td3.setAttribute("class", "dataCell");
newRow.appendChild(td1);
newRow.appendChild(td2);
newRow.appendChild(td3);
tableBody.appendChild(newRow);
}
}
function generatePeople() {
var firstNames = ["Jack", "Will", "Josh", "Tom", "Sam", "Chloe", "Emily", "Sophie", "Lily", "Olivia"];
var surnames = ["Smith", "Jones", "Brown", "Taylor", "Johnson", "White"];
var locationNames = ["Canyonville", "Hailsmere", "Northpath", "Gracemont", "Gainsburgh", "Heathersmith"];
for (var i = 0; i < PEOPLECOUNT; i++) {
var name = firstNames[randInt(firstNames.length - 1)] + " " + surnames[randInt(surnames.length - 1)];
var location = location[randInt(locationNames.length - 1)];
var age = randInt(100);
var currentPeople = new People(name, location, age);
people.push(currentPeople);
}
loadPeopleIntoTable();
}
function loadPeopleIntoTable() {
for (var i = 0; i < PEOPLECOUNT; i++) {
var people = people[i];
var peopleRow = document.getElementById("ageRow" + i.toString(10));
var cells = peopleRow.getElementsByTagName("td");
for (var j = 0; j < cells.length; j++) {
if (cells[j].hasChildNodes()) {
cells[j].removeChild(cells[j].childNodes[0]);
}
}
cells[0].appendChild(document.createTextNode(people.name));
cells[1].appendChild(document.createTextNode(people.location));
cells[2].appendChild(document.createTextNode(people.age.toString(10)));
}
}
function randInt(maxVal) {
return Math.floor(Math.random() * (maxVal + 1));
}
function commonFirstName() {
var tempArray = [];
var fName;
var array = ['John Smith', 'Jane Smith', 'John Black'];
for (i = 0; i < array.length; i++) {
fName = array[i].split(' ').slice(0, -1).join(' ');
tempArray.push(fName);
}
var mostCommon;
var occurences = 0;
for (j = 0; j < tempArray.length; j++) {
var tempName = tempArray[j];
var tempCount = 0;
for (k = 0; k < tempArray.length; k++) {
if (tempArray[k] == tempName) {
tempCount++;
}
if (tempCount > occurences) {
mostCommon = tempName;
occurences = tempCount;
}
}
}
alert(mostCommon + " : " + occurences);
}
现在这适用于函数中的数组 fullNames,但不适用于人员数组,该数组由具有名称、位置和年龄的 People 对象组成(如开头所示)。我只需要传递该数组,这样我就可以拆分元素-_-