我有个同学在显示页面的列表,我存储的学生信息与标识和名称的对象。
I have list of students displaying in a page and I am storing the student information as object with id and name.
我想添加的搜索框来搜索的学生,如果我得到列表中的滚动条。
I want to add search box to search students if I got a scroll bar in the list.
和根据搜索字符串更新的学生名单。
And update the students list according to the search string.
现在我遍历学生对象数组,并检查其名称中搜索字符串的索引。
Right now i am iterating student object array and checking the index of the search string in the name.
有没有更好的算法来提高性能。
Is there any better algorithm to increase the performance.
//我的code:
搜索=功能(数据,查询字符串) { VAR解析度=新阵列(); 对于(STU数据){ 如果(stu.name.search(查询字符串)!= -1){ res.push(STU); } } 返回水库; }
search = function(data,queryString) { var res = new array(); for(stu in data){ if(stu.name.search(queryString) != -1){ res.push(stu); } } return res; }
推荐答案您可以建立一个排序的指标,并使用二进制搜索。多个指数,如果你需要多个条件,例如,要查找名称或ID。更简单的实现比一棵树。
You can build a sorted index and use binary search. Multiple indices if you need to search by multiple criteria, e.g. name or ID. Simpler to implement than a tree.