Tuesday, 3 April 2018

Apply filter to any array of objects in Jquery

There are two ways of doing it.

1. You may use jQuery.grep():


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

2.You can do this very easily with the [].filter method:


var filterednames = names.filter(function(obj) {
    return (obj.name === "Joe") && (obj.age < 30);
});
You will need to add a shim for browsers that don't support the [].filter method: this MDN page gives such code.