jquery - Filter javascript object array by string array -
i have array of objects, this:
var companies = [ { "name" : "company 1", "logo" : "/logo.gif" }, { "name" : "company 2", "logo" : "/logo2.gif" }, { "name" : "company 3", "logo" : "/logo3.gif" } ];
i want filter array values have name exists in array:
var mycompanies = [ "company 1", "company 3" ];
in example, data returned be:
var companies = [ { "name" : "company 1", "logo" : "/logo.gif" }, { "name" : "company 3", "logo" : "/logo3.gif" } ];
what's best way this?
you can use $.grep()
new, filtered array, this
var result = $.grep(companies, function(e) { return $.inarray(e.name, mycompanies) != -1; });
you can test here. note performs better $.each()
loop, can test here: http://jsperf.com/each-vs-grep
Comments
Post a Comment