json对象数组上的jquery grep

sha*_*er1 6 jquery json getjson

我试图使用grep过滤json对象数组,以便搜索数组,如果任何键#2-6的值为yes,则返回键1和7的值.

数组在下面 - 换句话说,如果'location'键的任何值为yes,则名称和描述将作为列表项返回.

很感谢任何形式的帮助.

[
    {
        "name": "name",
        "location1": "no",
    "location2": "no",
    "location3": "yes",
    "location4": "no",
    "location5": "no",
    "description": "description of services"
    },

    {   
    "name": "name",
        "location1": "yes",
    "location2": "no",
    "location3": "yes",
    "location4": "no",
    "location5": "no",
    "description": "description of services"        
    }
]
Run Code Online (Sandbox Code Playgroud)

mVC*_*Chr 13

你需要同时使用grepmap.如果a是上述的阵列(但是name1,name2等),在此之后执行以下操作:

var b = $.grep(a, function(el, i) {
    return el.location1.toLowerCase() === "yes" 
           || el.location2.toLowerCase() === "yes" 
           || el.location3.toLowerCase() === "yes" 
           || el.location4.toLowerCase() === "yes" 
           || el.location5.toLowerCase() === "yes";
});

var c = $.map(b, function(el, i) {
    return {
        name: el.name,
        description: el.description
    };
});
Run Code Online (Sandbox Code Playgroud)

c 将包含 [{"name":"name1","description":"description of services1"},{"name":"name2","description":"description of services2"}]

见例→