2012-04-25 65 views
0

在jQuery的工作或普通的JavaScript選擇用指定的值

如果我有對象的這樣一個數組中的所有對象:

[ 
    {"company":"acme", "Employees":10, "location":"Cork"}, 
    {"company":"foo", "Employees":50, "location":"Limerick"}, 
    {"company":"bar", "Employees":10, "location":"Dublin"} 
    {"company":"zanzo", "Employees":23, "location":"Dublin"} 
    ] 

是有一個方便的辦法,我查詢這個數據?

例如讓所有的公司都擁有超過20 employess,或讓位於都柏林的所有公司提前

回答

2

您可以使用.grep()功能

感謝。例如:

var companiesWithOver20Employees = $.grep(companies, function(company, index) { 
    return company.Employees > 10; 
}); 

或:

var companiesInDublin = $.grep(companies, function(company, index) { 
    return company.location == 'Dublin'; 
}); 
+0

非常感謝您的回覆。 – 2012-04-25 15:30:50