JQuery filter of an object with a loop

I have an array of such objects:

myArray = [ {label: "a", value: "100"}, {label: "b", value: "101"}, {label: "c", value: "102"} ... 

I want to filter it as follows:

 myArrayFiltered = myArray.filter(function(v){ return v["value"] == "101" || v["value"] == "102"}); 

What will return

 myArrayFiltered = [ {label: "b", value: "101"}, {label: "c", value: "102"}] 

in this example, but I want to make a filter with an array of values. How can i do this?

+7
javascript jquery arrays filter
source share
3 answers

Just check if the value you are filtering is in your array

 myArrayFiltered = myArray.filter(function(v){ return ["102", "103"].indexOf(v.value) > -1; }); 
+5
source share

You can use . some method inside the filter:

 var requiredValues = ["101", "102", "103"]; myArrayFiltered = myArray.filter(function(v){ return requiredValues.some(function(value) { return value === v.value; }); }); 
0
source share
 var arrValues = ["101", "102"]; var result = getData(arrValues,"102") function getData(src, filter) { var result = jQuery.grep(src, function (a) { return a == filter; }); return result; } 
0
source share

All Articles