在客户端搜索对象?

发布于 2024-12-17 07:49:41 字数 105 浏览 2 评论 0原文

我的客户端内存中有 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

歌枕肩 2024-12-24 07:49:41

元素

如果您想在元素内查找文本,请尝试以下操作:

$(":contains('your text')");

这将返回包含您的文本的每个元素。

对象

请参阅此演示:http://jsfiddle.net/datyn/1/

同样搜索子对象,目前搜索的是Case Insensitive,如果要更改,只需去掉.toLowerCase()函数即可:

var ob = {
    User : {
        name : "Niels",
        country : "Netherlands"
    },
    Name : "Niels test X"
}

function find_match(search, results)
{
    $.each(this, function(k, v){
       if( typeof(v) == "object" )
       {
            find_match.call(v, search, results);  
       }
        else
        {
             if( v.toLowerCase().indexOf(search.toLowerCase()) != -1)
             {
                 if($.inArray(this, results) == -1)
                 {
                     results.push(this);
                 }   
             }
        }
    });
}

var results = [];
find_match.call(ob, "x", results);
alert("Search for x results: " + results.length);
var results = [];
find_match.call(ob, "n", results);
alert("Search for n results: " + results.length);

可以使用.call调用该函数代码>方法。

示例:

find_match.call("Object / array you want to search", "The string", "Array where the results will be stored")

更改:

  1. 如果您不想匹配字符串的一部分,请将 v.toLowerCase().indexOf(search.toLowerCase()) != -1 更改为 v .toLowerCase() == search.toLowerCase()

Elements:

If you want to look for text within elements, try this:

$(":contains('your text')");

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:

var ob = {
    User : {
        name : "Niels",
        country : "Netherlands"
    },
    Name : "Niels test X"
}

function find_match(search, results)
{
    $.each(this, function(k, v){
       if( typeof(v) == "object" )
       {
            find_match.call(v, search, results);  
       }
        else
        {
             if( v.toLowerCase().indexOf(search.toLowerCase()) != -1)
             {
                 if($.inArray(this, results) == -1)
                 {
                     results.push(this);
                 }   
             }
        }
    });
}

var results = [];
find_match.call(ob, "x", results);
alert("Search for x results: " + results.length);
var results = [];
find_match.call(ob, "n", results);
alert("Search for n results: " + results.length);

You can call the function by using .call method.

Example :

find_match.call("Object / array you want to search", "The string", "Array where the results will be stored")

Changes:

  1. If you don't want to match a part of a string, change: v.toLowerCase().indexOf(search.toLowerCase()) != -1 into v.toLowerCase() == search.toLowerCase()
深海不蓝 2024-12-24 07:49:41

您可能想看看这个:

http://goessner.net/articles/JsonPath/

You might want to take a look at this:

http://goessner.net/articles/JsonPath/

绮烟 2024-12-24 07:49:41

对于集合中的每个对象(我假设它们可能保存在一个数组中,或者是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文