Kevin Bradshaw
Kevin Bradshaw

Reputation: 6427

Select all objects with a specified value

Working in jQuery or plain javascript

If i have an array of objects like so:

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

is there a convenient way for me to query this data?

e.g. get all companies with over 20 employess, or get all companies located in Dublin

Thanks in advance

Upvotes: 0

Views: 49

Answers (1)

Darin Dimitrov
Darin Dimitrov

Reputation: 1038780

You could use the .grep() function. For example:

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

or:

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

Upvotes: 2

Related Questions