从和数组中选择唯一单词时出现问题



我有一个由两个关键字组成的数组(很快就会有更多(,当键入一个单词时,我会有一个预先写好的响应。因此,我面临的问题是由于有相似的词语。我的数组中存储了两个项目,邮件管理员邮件件响应。如果有人能帮忙,将不胜感激

keywords=new Array();
urls=new Array();
replymsg=new Array();
keywords[0]="mail";
urls[0]="mail.html";
replymsg[0]="Type yes and I will load the mail app!"; 
keywords[1]="adminmail";
urls[1]="adminmail.html";
replymsg[1]="Type yes and I will load the admin mail app!";

function reply() {
wordentered=document.myform.user.value.toLowerCase();
myurl="";
for (i=0;i<keywords.length;i++) {
if (wordentered.indexOf(keywords[i])>-1) {
myurl=urls[i];
makecomputersay(replymsg[i]);
return;
}
}
}

"wordentered.indexOf(keywords[i](&gt-1〃;将满足邮件和;adminmail。您可以使用相等运算符进行比较。下面是这样的。

for (i=0;i<keywords.length;i++) {
if (wordentered === keywords[i]) {
myurl=urls[i];
makecomputersay(replymsg[i]);
return;
}
}
}

请在此处查看indexOf的工作方式:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/indexOf

您可能会发现使用对象更容易。这样,您就可以通过它的键访问所需的信息,而不必在数组上循环。1( 它看起来更整洁它更有效率。3( 您可以使用此格式obj[word].urlobj[word].replymsg访问信息。

注意:您还应该开始使用letconst正确地声明您的变量

// New object containing some sub-objects
// You can access each bit of information
// with obj[word].url etc
const obj = {
mail: {
url: 'mail.html',
replymsg: 'Type yes and I will load the mail app!'
},
adminmail: {
url: 'admilemail.html',
replymsg: 'Type yes and I will load the admin mail app!'
}
};
// Because you're new to JS you may not be
// familiar with the next terminology. I'm just using them to
// make the example work, but you will have to understand them
// at some point, so why not now.
// `querySelector` (link to documentation at the end of the
// answer) simply uses CSS selectors to pick up elements from the DOM
const wordentered = document.querySelector('input[name="user"]');
const button = document.querySelector('button')
// And I'm adding a simple click listener to the button
// that calls the `reply` function
button.addEventListener('click', reply, false);
// Log the message
function makecomputersay(msg) {
console.log(msg);
}
function reply() {

// Get the value from the input
const word = wordentered.value.toLowerCase();

// If that word exists as a key on the object...
// (No loop required!)
if (obj[word]) {

// ...get the message
const replymsg = obj[word].replymsg;

// Call the logging function
makecomputersay(replymsg);
}
}
<input name="user" type="text">
<button type="button">Submit</button>

后记:

这可能有点高级,但我想我应该提到它。你也可以使用一种名为Destructuring赋值的东西来从对象中获取值。

所以不是:

const url = obj[word].url;
const replymsg = obj[word].replymsg

您可以将这两个语句合并为一行:

const { url, replymsg } = obj[word];

附加文件

  • querySelector

  • addEventListener

仅添加到上一个答案:

wordentered.indexOf(keywords[i](不是正确的方法。这样做的目的是,它会检查关键字在您输入的单词中出现的位置。

在您的示例中,这意味着:

wordentered = "adminmail"
keywords[0] = "mail"
// keyword "mail" appears at index 5 (6th letter) of wordentered
// wordentered.indexOf("mail") === 5

wordentered = "adminmail"
keywords[1] = "adminmail"
// keyword "adminmail" appears at index 0 (1st letter) of wordentered
// wordentered.indexOf("adminmail") === 0

所以在这两种情况下,wordentered.indexOf(keywords[i](都大于-1。如前所述,这里的方法是使用等于运算符。

最新更新