我有一系列对象,我想知道搜索它的最佳方法。给定以下示例,我如何搜索name = "Joe"和age < 30?jQuery有什么可以帮助的吗?还是我必须自己蛮力搜索?
name = "Joe"
age < 30
var names = new Array(); var object = { name : "Joe", age:20, email: "joe@hotmail.com"}; names.push(object); object = { name : "Mike", age:50, email: "mike@hotmail.com"}; names.push(object); object = { name : "Joe", age:45, email: "mike@hotmail.com"}; names.push(object);
您可以使用jQuery.grep():
jQuery.grep()
var found_names = $.grep(names, function(v) { return v.name === "Joe" && v.age < 30; });