javascript - trouble with a search function -
function todo(id, task, who, duedate) { this.id = id; this.task = task; this.who = who; this.duedate = duedate; this.done = false; } // more code adds todo objects page , array todos function search() { (var = 0; < todos.length; i++) { var todoobj = todos[i]; console.log(todoobj.who); //shows both jane , scott console.log(todoobj.task); // shows both , 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 come in string search for"); return; } else { alert(results); } }
this search function trying match user types search bar objects have created before in code. must match "who" , "task" parameters have given objects. 1 object who: jane task: , other who: scott task: milk. problem is, in lastly alert can match scott , not jane. scott lastly 1 added. there way need modify loop or alter search criteria?
your problem looping through items, using todoobj
after loop. todoobj
hold lastly item in array. need reorganize little...try this:
function search() { var searchterm = document.getelementbyid("search").value; searchterm = searchterm.trim(); if (searchterm == null || searchterm == "") { alert("please come in string search for"); return; } else { var todoobj = undefined, results = undefined, re = new regexp(searchterm, "ig"); (var = 0; < todos.length; i++) { todoobj = todos[i]; results = todoobj.who.match(re); if (results) { alert("you found " + todoobj.who + ", needs " + todoobj.task + " " + todoobj.duedate); return; } console.log(re.lastindex); } alert("you didn't match anyone"); } }
here's illustration of working think want to: http://jsfiddle.net/shsdk/2/
javascript regex loops match
No comments:
Post a Comment