最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Jquery filter array of object with loop - Stack Overflow

programmeradmin1浏览0评论

I have an array of objects like this:

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

I want to filter it like this:

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

Which will return

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

in this example but I want to do the filter with an array of values. How can I do that ?

I have an array of objects like this:

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

I want to filter it like this:

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

Which will return

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

in this example but I want to do the filter with an array of values. How can I do that ?

Share Improve this question asked Jun 23, 2015 at 9:05 GtAntoineGtAntoine 1,0891 gold badge8 silver badges12 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 5

Just check if the value you're filtering on is in your array

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

You could use the .some method inside your filter:

var requiredValues = ["101", "102", "103"];
myArrayFiltered = myArray.filter(function(v){ 
    return requiredValues.some(function(value) {
        return value === v.value;
    });
});
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)

  1. 暂无评论