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);
}
}
これは、ユーザーが検索バーに入力した内容を、コードの前半で作成したオブジェクトと一致させようとしている検索機能です。それらは、私がオブジェクトに与えた「who」および「task」パラメーターと一致する必要があります。つまり、1つのオブジェクトはwho:ジェーンタスク:何かを実行し、もう1つのオブジェクトはwho:scottタスク:ミルクを取得することです。問題は、私の最後のアラートでは、ジェーンではなくスコットにしか一致できないことです。スコットは私が最後に追加したものです。ループを変更したり、検索条件を変更したりする必要がある方法はありますか?