[英]check if a part of an array element contains certain string
Assuming an array containing student's names and their respective score, I want to retrieve the score of certain student (by checking if the array element contains the student's name (string)) 假设一个数组包含学生的姓名和他们各自的分数,我想检索某些学生的分数(通过检查array元素是否包含学生的姓名(字符串))
I came up with this but it doesn't work 我想出了这个办法,但是没有用
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>
You are doing students[i].toString()
. 您正在做
students[i].toString()
。 This will return only a string which contains the items of the array. 这将仅返回包含数组项的字符串。 So suggest you don't do the
toString
and iterate over each item and use indexing. 因此,建议您不要执行
toString
并遍历每个项目并使用索引。 And also inside the first array you can contain objects
of students
, it will be more logically. 而且在第一个数组内,您可以包含
students
objects
,这将更加合乎逻辑。
Move the 移动
var namebox = document.getElementById("namebox");
out of the for loop
. 退出
for loop
。 Every time engine spend a time to find the element in the for loop. 每次引擎花费时间在for循环中查找元素。 So you get it once and use many times.
因此,您一次获得并多次使用。
ES6 syntax 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); } }); }
<input type="text" id="namebox" /> <button type="button" onclick="test()">Click</button>
ES5 syntax 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); } } }
<input type="text" id="namebox" /> <button type="button" onclick="test()">Click</button>
Your code should work, except for the syntax error in the if
condition, which has a ;
您的代码应该可以正常工作,但
if
条件中的语法错误除外,该条件错误带有;
that should not be there. 那不应该在那里。 Remove it and it works:
删除它,它的工作原理是:
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>
But there are some inefficiencies in your code: 但是您的代码中存在一些效率低下的问题:
students[i]
to string, it seems more to the point to just look at students[i][0]
, which is a string already students[i]
转换为字符串,不如只看一下students[i][0]
,这已经是一个字符串了。 .includes()
and thus are running on a modern browser, why not use the other features available in ES6, like .find()
? .includes()
并因此在现代浏览器上运行,因此为什么不使用ES6中的其他功能,如.find()
呢? Here is the same with more efficient ES6 code: 这与更有效的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]); } };
<input type="text" id="namebox" /><button type="button" onclick="test()">Click</button>
You could use Array#filter
and return an array with the matched names. 您可以使用
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]];
<input type="text" id="namebox" /><button type="button" onclick="getStudentsByName()">Click</button> <pre id="out"></pre>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.