JSON对象数组上的jQuery grep [英] jquery grep on json object array

查看:78
本文介绍了JSON对象数组上的jQuery grep的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用grep过滤json对象数组,以便搜索该数组,并且如果键#2-6中的任何一个的值是yes,则返回键1和7的值.

Im trying to use grep to filter a json object array so that the array is searched and if the value of any of keys #2-6 are yes, the value of keys 1 and 7 are returned.

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

The array is below -- in other words, if any of values for the 'location' keys are yes, the name and description are returned as list items.

非常感谢您的帮助.

[
    {
        "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"        
    }
]

推荐答案

您将需要同时使用grepmap.如果a是上述数组(但带有name1name2等),则执行以下操作:

You will need to use both grep and map. If a is the array described above (but with name1, name2, etc), then after the following:

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
    };
});

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

> 请参见示例→

这篇关于JSON对象数组上的jQuery grep的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆