在客户端搜索对象?
我的客户端内存中有 50-100 个对象。我需要在没有标签的情况下搜索它们,只需文本搜索对象的每个字段并查找匹配或部分匹配。
进行此类搜索的最佳方式是什么?如何根据相关性列出它们?
I have 50-100 objects in memory client-side. I need to search them without tags, just text searching every field of the object and finding matches or partial matches.
What is the best way to do this type of search, how can I list them based on relevance?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
元素:
如果您想在元素内查找文本,请尝试以下操作:
这将返回包含
您的文本
的每个元素。对象
请参阅此演示:http://jsfiddle.net/datyn/1/
同样搜索子对象,目前搜索的是Case Insensitive,如果要更改,只需去掉
.toLowerCase()
函数即可:可以使用
.call
调用该函数代码>方法。示例:
更改:
v.toLowerCase().indexOf(search.toLowerCase()) != -1
更改为v .toLowerCase() == search.toLowerCase()
Elements:
If you want to look for text within elements, try this:
This will return each element that contains
your text
.Objects
See this demo: http://jsfiddle.net/datyn/1/
Also searches for sub objects, currently searches Case Insensitive, if you want to change it, just remove the
.toLowerCase()
functions:You can call the function by using
.call
method.Example :
Changes:
v.toLowerCase().indexOf(search.toLowerCase()) != -1
intov.toLowerCase() == search.toLowerCase()
您可能想看看这个:
http://goessner.net/articles/JsonPath/
You might want to take a look at this:
http://goessner.net/articles/JsonPath/
对于集合中的每个对象(我假设它们可能保存在一个数组中,或者是 jQuery 选择的结果中的 50-100 个),使用
Object.keys
获取属性名称,然后获取关联的重视并执行您的比赛。由于您的匹配返回相关性分数,因此您可以将所有匹配放入一个成对的数组中(匹配 x 分数),并使用设置的比较器进行数组排序以按分数进行比较。
For each object in your collection (with 50-100 I assume they are kept in an array perhaps, or the result of a jQuery selection), use
Object.keys
to get the property names then grab the associated value and perform your match.Since your match returns a relevance score, you can place all matches into an array of pairs (match x score) and do an array sort with a comparator set to compare by score.