gpt4 book ai didi

javascript - "JavaScript objects can be indexed by strings"是什么意思?

转载 作者:行者123 更新时间:2023-11-30 15:59:13 26 4
gpt4 key购买 nike

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. Occurs 2 times”的警报输出

我曾想过,如果我只是简单地通过函数传递数组 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 数组,它由具有名称、位置和年龄的 People 对象组成(如开头所示)。我只需要传递该数组,这样我就可以拆分元素 -_-

最佳答案

“JavaScript objects can be indexed by strings”是指JavaScript中的对象就像一个哈希表。方法/字段名称只是该表 object.anyName 中的一个字符串键,可以写成 object['anyName']

对于您的练习,您可以使用它来创建常用名称的计数器。

因为这是一个练习,我不会给你完整的答案;)只是想法:

  • 对于数组中的每一项,取人名。
  • 使用人名作为“表”的键,如果该人名已经存在,则将其加一到计数器。
  • 最后你会得到一对名字/出现

如果您对这个练习很懒惰...请查看 lodash countBy 函数的源代码 ( https://github.com/lodash/lodash/blob/4.13.1/lodash.js#L8373 ),它可以满足您的需求。

关于javascript - "JavaScript objects can be indexed by strings"是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38025007/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com