假设一个数组包含学生的姓名及其各自的分数,我想检索某个学生的分数(通过检查数组元素是否包含学生的姓名(字符串))
我想出了这个,但它不起作用
var students = [
['David', 80],
['Dane', 77],
['Dick', 88],
['Donald', 95],
['Dean', 68]
];
function test() {
for (var i = 0; i < students.length; i++) {
var name = document.getElementById("namebox").value;
var string = students[i].toString();
if (string.includes(name);) {
alert(students[i][1]);
}
}
};
<input type="text" id="namebox" /><button type="button" onclick="test()">Click</button>
答案 0 :(得分:4)
你在做students[i].toString()
。这将只返回包含数组项的字符串。因此,建议您不要执行toString
并迭代每个项目并使用索引编制。而且在第一个数组中,您可以包含objects
students
,它将更符合逻辑。
移动
var namebox = document.getElementById("namebox");
for loop
。每次引擎花费一些时间在for循环中找到元素。所以你得到它一次并多次使用。
ES6语法
let students = [
{name: 'David', score: 80},
{name: 'Dane', score: 77},
{name: 'Dick', score: 88},
{name: 'Donald', score: 95},
{name: 'Dean', score: 68}
];
let namebox = document.getElementById("namebox");
function test(){
let name = namebox.value;
students.forEach(item => {
if(item.name.includes(name)){
console.log(name, item.name, item.score);
}
});
}
&#13;
<input type="text" id="namebox" />
<button type="button" onclick="test()">Click</button>
&#13;
ES5语法
var students = [
{name: 'David', score: 80},
{name: 'Dane', score: 77},
{name: 'Dick', score: 88},
{name: 'Donald', score: 95},
{name: 'Dean', score: 68}
];
var namebox = document.getElementById("namebox");
function test(){
var name = namebox.value;
for(var i = 0; i < students.length; i++){
if(students[i].name.indexOf(name) !== -1) {
console.log(name,students[i].name,students[i].score);
}
}
}
&#13;
<input type="text" id="namebox" />
<button type="button" onclick="test()">Click</button>
&#13;
答案 1 :(得分:1)
您的代码应该有效,但if
条件中的语法错误除外,其中;
不应该存在var students = [
['David', 80],
['Dane', 77],
['Dick', 88],
['Donald', 95],
['Dean', 68]
];
function test(){
for (var i = 0; i < students.length; i++) {
var name = document.getElementById("namebox").value;
var string = students[i].toString();
if (string.includes(name))
{
console.log(students[i][1]);
}
}
};
。删除它,它的工作原理:
<input type="text" id="namebox" /><button type="button" onclick="test()">Click</button>
&#13;
students[i]
&#13;
但是代码中存在一些效率低下的问题:
students[i][0]
转换为字符串,而不仅仅是查看.includes()
,这已经是一个字符串了.find()
并因此在现代浏览器上运行,为什么不使用ES6中提供的其他功能,例如const students = [
['David', 80],
['Dane', 77],
['Dick', 88],
['Donald', 95],
['Dean', 68]
];
const box = document.getElementById("namebox");
function test(){
const value = box.value;
const match = students.find(student => student[0].includes(value));
if (match) {
console.log(match[1]);
}
};
?以下是更高效的ES6代码:
<input type="text" id="namebox" /><button type="button" onclick="test()">Click</button>
&#13;
UIView
&#13;
答案 2 :(得分:1)
您可以使用Array#filter
并返回一个匹配名称的数组。
function getStudentsByName() {
var name = document.getElementById("namebox").value.toLowerCase();
document.getElementById("out").innerHTML = JSON.stringify(students.filter(function (a) {
return a[0].toLowerCase() === name;
}), 0, 4);
}
var students = [['David', 67], ['David', 80], ['Dane', 77], ['Dick', 88], ['Donald', 95], ['Dean', 68]];
&#13;
<input type="text" id="namebox" /><button type="button" onclick="getStudentsByName()">Click</button>
<pre id="out"></pre>
&#13;