在JavaScript中过滤JSON对象列表的最高性能方法是什么? [英] What is the highest performance way to filter a list of JSON objects in JavaScript?

查看:228
本文介绍了在JavaScript中过滤JSON对象列表的最高性能方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  [{name:'john dow ',年龄:38,性别:'m'},{姓名:'jane dow',年龄:18,性别:'f'},..] 

我想按名称过滤这个列表(字符明智)。

  filter('j')=> [姓名:'john dow',年龄:38,性别:'m'},{姓名:'jane dow',年龄:18,性别:'f'},..] 

过滤器('jo')=> ['name:'john dow',age:38,gender:'m'},..]

filter('dow')=> [姓名:'john dow',年龄:38,性别:'m'},{姓名:'jane dow',年龄:18,性别:'f'},..]

最高性能的方法是什么? RegEx显然是其中一个关键,如果您认为用户通常从一开始就倾向于从头开始名字,那么预先排列列表可能也是一个好主意,但它只在某些情况下有帮助。



有没有JavaScript内置函数来映射过滤器?我希望这些比JavaScript实现更快。



PS:是的,我想过滤客户端,因为我想提供离线功能。尽管子字符串索引 >(比如一个后缀树)会使这个更快,直接搜索将是:

  function(s,l){
return l.filter(function(v){
return v.name.find(s )!== -1;
});

$ / code>

其中 s 是查询字符串和 l 是对象列表。


Let's assume I have a huge (1000+) list of objects like this:

[{name: 'john dow', age: 38, gender:'m'}, {name: 'jane dow', age: 18, gender:'f'}, ..]

I want to filter this list by name (character wise).

filter('j') => [{name: 'john dow', age: 38, gender:'m'}, {name: 'jane dow', age: 18, gender:'f'}, ..]

filter('jo') => [{name: 'john dow', age: 38, gender:'m'}, ..]

filter('dow') => [{name: 'john dow', age: 38, gender:'m'}, {name: 'jane dow', age: 18, gender:'f'}, ..]

What is the highest performance way to do that? RegEx is obviously one of the keys, ordering the list beforehand if you assume that user usually tend to start names from the beginning may also a good idea, but it only helps in some cases.

Are there any JavaScript built-in functions for mapping a filter? I'd expect those to be faster than JavaScript implementations.

P.S.: Yes I want to filter on client side because of "offline capabilities" I want to offer.

解决方案

Although a substring index (such as a Suffix tree) would make this faster, the direct search would be:

function (s, l) {
    return l.filter(function (v) {
        return v.name.find(s) !== -1;
    });
}

where s is the query string and l is the list of objects.

这篇关于在JavaScript中过滤JSON对象列表的最高性能方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆