function Todo(id, task, who, dueDate) {
this.id = id;
this.task = task;
this.who = who;
this.dueDate = dueDate;
this.done = false;
}
// more code that adds the todo objects to the page and to the array todos
function search() {
for (var i = 0; i < todos.length; i++) {
var todoObj = todos[i];
console.log(todoObj.who); //shows both jane and scott
console.log(todoObj.task); // shows both do something and get milk
}
var searchTerm = document.getElementById("search").value;
searchTerm = searchTerm.trim();
var re = new RegExp(searchTerm, "ig");
var results = todoObj.who.match(re);
if (searchTerm == null || searchTerm == "") {
alert("Please enter a string to search for");
return;
} else {
alert(results);
}
}
这是一个搜索功能,我试图将用户在搜索栏中输入的内容与我之前在代码中创建的对象进行匹配。它们必须与我为对象提供的“谁”和“任务”参数相匹配。所以一个对象是谁:简任务:做某事,另一个对象是谁:斯科特任务:拿牛奶。问题是,在我上次的警报中,我只能匹配 scott 而不是 jane。斯科特是我添加的最后一个。有什么方法我需要修改我的循环或更改我的搜索条件吗?