最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

更好的搜索算法,以提高性能?

SEO心得admin89浏览0评论
本文介绍了更好的搜索算法,以提高性能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有个同学在显示页面的列表,我存储的学生信息与标识和名称的对象。

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.

发布评论

评论列表(0)

  1. 暂无评论