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.
}
给出一个警告输出"Common Name: John"。出现2次
我曾经想过,如果我简单地通过函数传递数组成员,比如:
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对象组成(如开头所示)。我只需要把数组传递过去这样我就可以分割元素了-_-
"JavaScript对象可以通过字符串进行索引"意味着JavaScript中的对象就像一个哈希表。方法/字段名(它只是表object.anyName
中的一个String键)可以写成object['anyName']
。
对于您的练习,您可以使用它来创建一个常用名称的计数器。
因为这是一个练习,我不会给你完整的答案;)只是想法:
- 对于数组中的每个项,取人名。
- 使用人名作为"表"的键,如果名字已经存在,则向计数器加1。
- 最后你会有一对名字/出现
如果你对练习很懒……查看lodash countBy函数的源代码(https://github.com/lodash/lodash/blob/4.13.1/lodash.js#L8373),它可以满足您的需要。
关于您的编辑:将数组作为参数传递工作得很好,您不需要在函数中包含数组:
var fullNames = ['John Smith', 'Jane Smith', 'John Black'];
function commonFirstName(array) {
var tempArray = [];
var fName;
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);
}
commonFirstName(fullNames);
检查小提琴:https://jsfiddle.net/d2m105pb/
一切都好,我已经找到了问题所在。由于组成数组People的对象,您将需要调用对象内部的特定变量—例如People [i].name。而不是人[它]本身。
感谢您的所有输入:)