【发布时间】:2010-12-27 04:28:55
【问题描述】:
我有一个这样的数组:
var anArray = [
{ name: "scala", type: "a" },
{ name: "abc", type: "b" },
{ name: "test", type: "a" },
{ name: "ruby", type: "c" },
{ name: "erlang", type: "a" },
];
我想根据 item 属性查找项目。我目前使用 jQuery 来做这件事。像这样的东西;
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 中做到这一点?还是有一些算法可以更好更快地做到这一点?当数组有很多项时。这可能很慢。有任何想法吗?谢谢。
【问题讨论】:
标签: javascript json search