javascript filter array of objects

arrays, javascript, jquery

Solution

A modern solution with `Array.prototype.filter()`:

const found_names = names.filter(v => v.name === "Joe" && v.age < 30);

Or if you still use jQuery, you may use `jQuery.grep()`:

var found_names = $.grep(names, function(v) {
    return v.name === "Joe" && v.age < 30;
});

Problem

I have an array of objects and I'm wondering the best way to search it. Given the below example how can I search for `name = "Joe"` and `age < 30`? Is there anything jQuery can help with or do I have to brute force this search myself? ``` 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); ```

Original source