You are doing 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. And also inside the first array you can contain objects of students, it will be more logically.
Move the
var namebox = document.getElementById("namebox");
out of the for loop. Every time engine spend a time to find the element in the for loop. So you get it once and use many times.
ES6 syntax
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
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>
property_name:property_valueformat,then you can useindexOfon property_name.