根据item属性值查找数组中某些项的最佳方法是什么? [英] what is the best way to find some items in an array based on the item property value?

查看:86
本文介绍了根据item属性值查找数组中某些项的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样一个数组:

var anArray = [
    { name: "scala",  type: "a" },
    { name: "abc",    type: "b" },
    { name: "test",   type: "a" },
    { name: "ruby",   type: "c" },
    { name: "erlang", type: "a" },
];

我想根据商品属性查找商品。我目前使用jQuery来做。像这样;

I want to find items based on the item property. I currently do it using jQuery. something like this;

Array.prototype.find_by_key = function(key, value) {
    return $.grep(this, function(item){
        return (item[key] == value);
    });
}

var whatIHaveFound = anArray.find_by_key("type", "a"); // find items which the item property: "type" equals "a"

是否有更好的方法在javascript中执行此操作?还是有一些算法可以更快更好地完成这项工作?当数组有很多项。这可能很慢。有任何想法吗?谢谢。

is there some better way to do this in javascript? or is there some algorithm to do this better and quickly? when the array has many items. this may be very slow. any ideas? thanks.

推荐答案

更聪明的头脑可能会纠正我,但我认为你每次都要进行迭代(在或多或少类似的速度),除非:

Wiser minds may correct me, but I would think you're going to have to iterate through every time (at more or less similar speeds), unless:

您知道您将重复搜索并且某些密钥比其他密钥更有可能被搜索。您可能会偏向该集合,因此它不是平坦的,而是根据您的条款进行预先排序。因此,您的收藏将变为:

You know that you're going to repeat searches and that some keys are more likely to be searched than others. You might bias the collection then so that it's not flat, but is presorted according to your terms. Thus your collection would become:

var groupedByType = 
{"a":[{name:"scala"},{name:"test"}, {name:"erlang"}],  
{"b":[{name:"abc"}],  
{"c":[{name:"ruby"}]};  

或者,您可以尝试记忆您的收藏访问权限,以便在完成搜索后记录搜索字词和结果,在这种情况下将其存储在备忘录缓存中

Alternatively you might try memoizing your collection access, such that after you've done a search you record the search terms and results, in this case storing it in the memo cache as

["type","a"]:[
{ name: "scala",  type: "a" },  
{ name: "test",   type: "a" },  
{ name: "erlang", type: "a" }]

优点和缺点:访问先前执行的搜索非常快,如果基础数据没有以弃用缓存的方式更改。另一方面,这意味着你必须控制数据访问(我喜欢在这些情况下将我的数据存储在一个闭包中,所以我只需要暴露mutators - 一种轻量级的OO,但有更多的保护),而你可能会发现,在修改数据时,丢弃缓存数据比尝试计算增量更高效,具体取决于数据的结构。

Pros and cons of this: It's very fast to access previously executed searches, if the underlying data has not changed in a way that deprecates your cache. On the other hand, it means that you have to control data access (I like to store my data inside a closure in these cases, so that I only have to expose mutators - sort of a lightweight OO but with more protection), and you may find it more performant to discard your cached data when it's modified than to try to work out deltas, depending on how your data is structured.

我要强调的是否则,就是要确保你完全需要任何优化。这个小节足够快吗?你能以另一种方式加速你的应用吗?如果您正在编写一个客户端搜索引擎或其他东西,那么您可能希望进一步了解它,而不仅仅是确定哪个jQuery迭代器最快。

What I would emphasize above everything else though, is to make absolutely sure that you need any optimization at all. Is this subsection fast enough? Can you bring your app up to speed another way? If you're writing a clientside search engine or something, you're probably going to want to go a little bit further into it than just working out which of the jQuery iterators is fastest.

这篇关于根据item属性值查找数组中某些项的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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