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对象可以用字符串索引"但我不明白这一点。我已经编写了代码来遍历数组并找到常用的名字。但是我不能在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次"
我曾想过,如果我简单地通过以下函数传递数组:
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对象(如开头所示)。我只需要传递的数组,所以我可以拆分元素-_-
答案 0 :(得分:2)
“JavaScript对象可以用字符串索引”意味着JavaScript中的对象就像一个哈希表。方法/字段名称只是该表object.anyName
中的字符串键,可以写为object['anyName']
。
对于您的锻炼,您可以使用它来创建常用名称的计数器。
因为这是一个练习,我不会给你完整的答案;)只是想法:
如果你对练习非常懒惰......看一下lodash countBy函数(https://github.com/lodash/lodash/blob/4.13.1/lodash.js#L8373)的源代码,它可以满足您的需求。
答案 1 :(得分:0)
关于你的编辑:传递数组作为参数工作正常,你不需要在函数内部有数组:
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);
答案 2 :(得分:0)
一切都很好,我已经弄清楚出了什么问题。由于构成数组People的对象,您需要在对象内调用特定变量 - 例如people [i] .name。而不仅仅是人[i]。
感谢您的所有输入:)